I have started learning promises and I wanted use it with mongoose. I have read that mongoose promises are primitive so I have applied to q
by using this code:
var mongoose = require('mongoose');
var q = require('q');
mongoose.Promise = q.Promise;
The I have created my schema and model and try to call functions via promises:
User.findOne({chat_id: 2}).exec().then(
function (obj) {
console.log("SUCCESS");
console.log(obj);
},
function (err) {
console.log("ERROR");
console.log(err);
}
).done();
When I call this code it always calls resolve
part and skips reject
part. My console shows always function which has SUCCESS
line even if I intentionally query for not existing data .
Am I missing something, or mongoose will always use Model.method().exec().then(resolveFunction)
?
Not finding something is not an error, it just means you did not find something. See: What is returned from Mongoose query that finds no matches? and likely your problem is related to this Mongoose JS findOne always returns null
Also try this: Note I have not worked with q.Promise, I use bluebird.
User.findOne({chat_id: 2}).exec().then(
function (obj) {
if (obj.chart_id === 2) {
console.log("SUCCESS");
console.log(obj);
} else {
console.log("obj with obj.chart_id == 2 NOT FOUND");
console.log(obj);
}
},
function (err) {
console.log("ERROR");
console.log(err);
}
).done();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.