Nodejs Mongoose Saving model undefined is not a function

。_饼干妹妹 提交于 2019-11-30 09:53:59

When you use in Mongoose the find method, it will return an array since it could discover one or many documents, so in your example you are querying to one specific element by its id, you should grab the first element on the returned array:

     Model.find({myId: myId}).exec(function (err, documents) {
            var model = documents[0];

            if (err) throw err;
            var fieldArray = model[0].fieldArray;

Here is an example:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost:27017/java-jedi');

var HackerSchema = new Schema({
  name: String,
  languages: [String]
});

var Hacker = mongoose.model('Hacker', HackerSchema);


// instantiating the model.
var oneHacker = new Hacker();
oneHacker.name = 'Richard Stallman';

oneHacker.save(function(err) {
  if (err) throw err;

  // finding the document intentionally for this example
  Hacker.find({_id: oneHacker._id}, function(err, hackers) {
    var hacker = hackers[0];

    // modifying the document and updating it.
    hacker.languages.push('Lisp');
    hacker.save(function(err) {
      if (err) throw err;

      console.log(hacker);
    });
  });

});
JavaJedi

OK guys! I want to thank Wilson Balderrama, because he basically pointed to the right direction.

The code works! But let me clearify a bit.

  Hacker.find({_id: oneHacker._id}, function(err, hackers) {
var hacker = hackers[0];

// modifying the document and updating it.
hacker.languages.push('Lisp');
hacker.save(function(err) {
  if (err) throw err;

  console.log(hacker);
});

});

So basically since the Model.find(.. returns an array
when we save we have to grab the thing from array before saving.

So corrected and final working version of my example will be:

    router.get('/:myId/:addThisToFieldArray', function(req, res, next) {
    var myId = req.params.myId;
    var addThisToFieldArray = req.params.addThisToFieldArray;
    Model.find({myId: myId}).exec(function (err, model) {
        if (err) throw err;
        var fieldArray = model[0].fieldArray;
        fieldArray.push("New thing to FieldArray");
        var newFieldArray = fieldArray;
        if (typeof newFieldArray === "object") model[0].fieldArray = newFieldArray;
        model[0].save(function (err, updatedModel){
            if (err) throw err;
            res.send(updatedModel);
        });
    });
});

Or we can use just Model.findOne(.. to avoid confusing ourselves with this arry return

In this case we grab directly:

router.get('/:myId/:addThisToFieldArray', function(req, res, next) {
var myId = req.params.myId;
var addThisToFieldArray = req.params.addThisToFieldArray;
Model.findOne({myId: myId}).exec(function (err, model) {
    if (err) throw err;
    var fieldArray = model.fieldArray;
    fieldArray.push("New thing to FieldArray");
    var newFieldArray = fieldArray;
    if (typeof newFieldArray === "object") model.fieldArray = newFieldArray;
    model.save(function (err, updatedModel){
        if (err) throw err;
        res.send(updatedModel);
    });
});
});

So in second case model[0].save(... becomes model.save(... direct grabbing and saving.

Thank you Wilson Balderrama again!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!