因此有一个完全独立的问题(我认为)将数组保存到mongo文档中。所以我拿出那个部分,创建了一个具有相同问题的数据结构来尝试解决它。但是现在我的测试代码似乎在save函数返回之前完成,只是不确定如何解决这两个问题,或者它们是否可能已连接。
猫鼬模式:
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中存储一个值。
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?");
})
但是,代码似乎在保存完成之前完成,我实际上不确定如何避免这种情况。
答案 0 :(得分:1)
好的,发现了这个问题。 mongoose save没有被宣传(或者至少那是我调查的问题),更改了第一部分使用bluebird for promises,然后将save更改为使用saveAsync,如下所示 架构(以及连接和蓝鸟)
var mongoose = require('mongoose');
var Promise = require('bluebird');
Promise.promisifyAll(mongoose);
mongoose.connect('mongodb://webbetcha:M0t0rWrl3d@localhost/betchadb');
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);
代码
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
myOffer.saveAsync()
.then(function(doc){
console.log('Got an id: ' + myOffer._id)
})
.catch(function(err){
if(err){
console.log('Got an error: ' + err)
}
});
答案 1 :(得分:1)
您正在将逻辑与回调和承诺混合在一起。所以你不需要回调,只需对返回的承诺采取行动:
myOffer._offered = undefined
myOffer.save()
.then(function() {
console.log("I get here and quit?");
})
.then(null,function(err) {
console.log(err);
});
我注意到您使用Bluebird承诺再次尝试,但没有必要像您在此处所示实现任何错误将被适当地路由。
Threre指出,mongooose 5.x预计将采用更标准化的承诺方式和/或在配置时直接使用Bluebird承诺