Mongoose Seems To Fail Quietly
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
Solution 1:
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);
});
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.
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
Solution 2:
OK, found the issue. mongoose save is not promisified (or at least that is what I recon the issue is), changed the first part to use bluebird for promises, then changed the save to use saveAsync as below Schema (and connection, and bluebird)
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);
And the code
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)
}
});
Post a Comment for "Mongoose Seems To Fail Quietly"