我正在使用bluebird的promisifyAll和猫鼬.当我在模型对象上调用saveAsync(保存的promisified版本)时,已完成的promise的解析值是一个包含两个元素的数组.第一个是我保存的模型对象,第二个是整数1.不知道这里发生了什么.下面是重现该问题的示例代码.
var mongoose = require("mongoose");
var Promise = require("bluebird");
Promise.promisifyAll(mongoose);
var PersonSchema = mongoose.Schema({
'name': String
});
var Person = mongoose.model('Person', PersonSchema);
mongoose.connect('mongodb://localhost/testmongoose');
var person = new Person({ name: "Joe Smith "});
person.saveAsync()
.then(function(savedPerson) {
//savedPerson will be an array.
//The first element is the saved instance of person
//The second element is the number 1
console.log(JSON.stringify(savedPerson));
})
.catch(function(err) {
console.log("There was an error");
})
Run Code Online (Sandbox Code Playgroud)
我得到的回应是
[{"__v":0,"name":"Joe Smith ","_id":"5412338e201a0e1af750cf6f"},1]
Run Code Online (Sandbox Code Playgroud)
我只期待该数组中的第一项,因为mongoose模型save()方法返回一个对象.
任何帮助将不胜感激!
我正试图在我的一个模型中有一个"可选的"嵌套文档.这是一个示例模式
var ThingSchema = mongoose.Schema({
name: String,
info: {
'date' : { type: Date },
'code' : { type: String },
'details' : { type: Object }
}
})
var Thing = mongoose.model('Thing', ThingSchema);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我希望"info"属性可能为null(不是必需的),并且将具有检查信息是否为null/undefined的应用程序逻辑.
不幸的是,即使在mongo的文档中未定义'info'属性,mongoose仍然会在检索到的模型中返回一个'info'字段的对象.
例如,当我运行此代码时,'info'对象填充了一个(空)对象.
var thingOne = new Thing({
name: "First Thing"
})
thingOne.save(function(err, savedThing) {
if (savedThing.info) {
//This runs, even though the 'info' property
//of the document in mongo is undefined
console.log("Has info!");
}
else console.log("No info!");
})
Run Code Online (Sandbox Code Playgroud)
我尝试像下面那样定义ThingSchema
var ThingSchema = mongoose.Schema({
name: String, …Run Code Online (Sandbox Code Playgroud)