在mongoose模式中的upsert throws promise没有方法'save'

时间:2014-01-25 20:54:20

标签: javascript node.js mongoose

我使用mongoose,我希望我的PersonSchema添加upsert方法 在PersonSchema.statics里面我添加了像

  upsert : function(obj, cb) {
    this.findOne({ _id : obj.id }, function(err, person) {
      console.log('inside find one');
      if(person !== null)
        {
          console.log('found person');
          this.update({'pid':obj.pid},obj,{upsert:true}, cb);
        }
        else
        {
            console.log('saving new person');
            var person  = new PersonSchema(obj);
            person.save(obj);
        }
    });
  },

obj是像{'_id' : '52e03681ecc7426f2c00000b', 'name' : 'Sally'}这样的字典 但我得到错误

promise has no method 'save'

如何在我的猫鼬模式中创建upsert方法?

1 个答案:

答案 0 :(得分:0)

Mongoose Schema没有名为save的方法。你能不能upsertModel#findOneAndUpdate一起使用?也会减少对Mongo的调用。