1

This is the Mongoose Schema:

var userschema = new mongoose.Schema({

   user: String,
   imagen: [{ 

              tags: [String],

           }]

});

I'm getting the tags with this:

var bodytags = req.body.tags;
var tags = tbags.split(" ");

I get the tags from req.body, and put all of them within an array. But the problems comes here:

var img = usermodel.findOne({ user: req.session.user }, function(err, user){

  var img = user.imagen.push({

     tags: undefined

  });
});

img.tags.push(tags);

And when, I received an error that sais TypeError: Cannot call method 'push' of undefined. How can I push my array of tags inside the tags' array of my Schema?

Thank's advance!

EDITED:

And this would be the result of what I want:

 {

     user: randomuser,
     imagen: [{
                tags: ["one", "two", "three"
              }] 

 }

And, if for example, there are two imagen objects:

  {

     user: randomuser,
     imagen: [{
                tags: ["one", "two", "three"]
              },
              {

                tags: ["four", "five", "six"]

              }] 

 }

1 Answer 1

1

Seems the error could be due to inconsistencies in variable assignment. So have created a stand-alone example to test and it works

 var mongoose = require('mongoose');
 mongoose.connect('mongodb://localhost/images');

 var UserSchema = new mongoose.Schema({
    user: String,
    imagen: [ {
             tags: [String]
          } ]
 });

 var User  = mongoose.model('User', UserSchema);

 var newUser = new User({ user: 'Almypal'});

 var bodytags = 'Home basic tutor';
 var tagarray = bodytags.split(" ");

 newUser.save(function(err, result){
   if(err)
   {
       console.log(JSON.stringify(err));
   }
   else
       {
           result.imagen.push({ tags: tagarray });
           result.save( function(error, data){
             if(error){
               console.log(JSON.stringify(error));
             }
             else{
               console.log(JSON.stringify(data));
             }
       });
    }
});

MongoDB now has the following document

 { "_id" : ObjectId("50e84425862a2af616000001"), "imagen" : [ { "tags" : [ "Home", "basic", "tutor" ] } ], "user" : "Almypal" }

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

It doen't work at all. I want to add some tags to an specific imagen, but in result.imagen.push({ tags: tagarray });, I add those tags, but not in the imagen that I want.
Could you update the question with a typical record/document that you expect... would help with the schema design.
Isn't the output given in the answer similar to what you expect. If you wish to add another array of tags to imagen just do a push again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.