猫鼬-不存在'保存'方法 [英] mongoose - 'save' method does not exist

查看:94
本文介绍了猫鼬-不存在'保存'方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑在MongooseJS上运行的mongodb集合.

Consider a mongodb collection running on MongooseJS.

Person.where('uid').equals(19524121).select('name').exec(function(err, data){
     // Here I can get the data correctly in an array.
     console.log(JSON.stringify(data)); 
     data[0].name = "try to save me now"; // Select the first item in the array
     data[0].save(); // Object #<Promise> has no method 'save'.
}

错误-似乎无法找到解决此问题的方法.

对象#<承诺>没有方法'save';

Object #<Promise> has no method 'save';

对于为什么会这样,我有些困惑,我已经进行了很多研究,似乎并没有找到直接的答案.

I am a little confused on why this is happening and I have researched quite a bit and can't seem to find a direct answer for this.

推荐答案

find的结果是一组记录.您可能打算像这样遍历这些记录:

The result of a find is an array of records. You probably meant to loop over those records like this:

Person.find({ uid: /19524121/ }).select('name').exec(function(err, data){
  for(var i = 0; i < data.length; i++) {
     var myData = new Person(data[i]);
     myData.name = "try to save me now";
     myData.save(); // It works now!
  }
}

此外,从猫鼬的主页看来,函数回调原型是function(err, data),而不是其他方式周围,​​您已在上面对其进行了修正.

Also, from the mongoose homepage, it appears that the function callback prototype is function(err, data), not the other way around, which you corrected above.

从首页查看此内容

var fluffy = new Kitten({ name: 'fluffy' });

如果data[0]当前具有常规JSON对象,则需要这样的行才能转换为BSON模型对象.

If data[0] currently has a regular JSON object, we need a line like this to convert to a BSON model object.

var myData = new Person(data[0]);

这篇关于猫鼬-不存在'保存'方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆