猫鼬似乎悄悄地失败了 [英] Mongoose seems to fail quietly

查看:41
本文介绍了猫鼬似乎悄悄地失败了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以有一个完全独立的问题(我认为)将数组保存到 mongo 文档.所以我去掉了那部分,创建了一个数据结构,试图解决这个问题.但是现在我的测试代码似乎在 save 函数返回之前完成,只是不确定如何解决任一问题,或者它们是否可能已连接.

So have a completely separate issue (I think) which is saving an array to a mongo document. So I took out that part, created a data structure that would have the same issue to try to resolve it. But now my test code seems to finish before the save function returns, just not certain how to resolve either issue, or if they potentially are connected.

猫鼬模式:

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

var offerSchema = mongoose.Schema({
    closeDate: Date,
    settleDate: Date,
    schemaVersion: Number,
    _offered: [{ type: ObjectId, ref: 'User'}], //Ids of thos offered
    _offerDate: { type: Date },// The date they where offered
    _accepted: [{ type: ObjectId, ref: 'User'}],//Ids of those accepted
    _acceptedDate: [{ type: Date }], //Dates when accepted
});



// create the model for users and expose it to our app
module.exports = mongoose.model('offer', offerSchema);

所以我写了下面的代码来重现这个问题,下面的代码应该可以工作,但不能在 _offered 中存储值.

So I have written the code below to recreate the issue, the below code should work but not store a value in _offered.

var offer = require('../models/offerSchema.js')
var indata = {"offer":{"closeDate":"2015-08-31T13:26:36.512Z","settleDate":"2015-08-31T13:26:36.512Z","type":1,"_offered":[{"id":"55dc7994ed0fcf4a58d4a689"},{"id":"55dcd30915e3be545a51bebd"}],"_offerDate":"2015-08-31T13:26:36.512Z"}}

var thisOffer = indata.offer
for ( var i in thisOffer ){
    console.log("Got "+ i +" is " + thisOffer[i])

}
var myOffer = new offer(thisOffer);
myOffer._offered = undefined
var promise =
myOffer.save(function(err){
    if(err){
        console.log('Got an error: ' + err)
    }
    console.log('Got an id: ' + myOffer._id)
    return 0;
}).then(function() {
    console.log("I get here and quit?");
})

然而,代码似乎在保存完成之前完成,我实际上不确定如何避免这种情况.

However, the code seems to complete before the save have completed, and I am actually not certain how to avoid this.

推荐答案

您将逻辑与回调和承诺混合在一起.所以你不需要回调,只需对返回的承诺采取行动:

You are mixing logic with callbacks and promises. So you do not need the callback, just act on the promise returned:

myOffer._offered = undefined
myOffer.save()
  .then(function() {
    console.log("I get here and quit?");
  })
  .then(null,function(err) {
      console.log(err);
  });

我注意到您再次尝试使用 Bluebird 承诺,但没有必要,如果您按照此处所示实施,任何错误都将被适当路由.

I noticed you made another attempt using Bluebird promises, but it is not necessary as if you implement as shown here any errors will be appropriately routed.

注意,mongooose 5.x 将采用更标准化的承诺方法和/或在配置时直接使用 Bluebird 承诺

Threre are notes that mongooose 5.x is slated to have a more standardised approach to promises and/or directly use Bluebird promises when configured

这篇关于猫鼬似乎悄悄地失败了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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