Node.js 猫鼬没有';救火电话
我很困惑。 安装的mongoose模块(最新版本)未启动保存回调 我定义了以下模式:Node.js 猫鼬没有';救火电话,node.js,mongodb,database-connection,Node.js,Mongodb,Database Connection,我很困惑。 安装的mongoose模块(最新版本)未启动保存回调 我定义了以下模式: var Object = new global.db.Schema({ customerId: { type: String, required: true }, customerPrefix: { type: String, required: true }, name: { type: String, required: true }, fields: { type: Array, defa
var Object = new global.db.Schema({
customerId: { type: String, required: true },
customerPrefix: { type: String, required: true },
name: { type: String, required: true },
fields: { type: Array, default: [] },
// ... (other fields have default values)
});
global.db是mongoose(server.js):
global.models包含模型(lib/dao/)
现在我尝试创建一个新文档:
var newObject = new global.models.Object({});
newObject.customerId = data.customerId;
newObject.customerPrefix = data.prefix;
newObject.fields = data.fields;
newObject.name = data.name;
console.log(global.db.connection.host); // localhost
console.log(global.db.connection.port); // 27017
// i've tested also whether i can call .find (it works and callback will fired!)
newObject.save(function(err) {
// this part will never called
console.log("save callback fired");
if (typeof callback == 'function') {
callback(err, newObject);
}
});
我完全不明白为什么没有启动回调。
测试一些已知问题(例如,连接未完成等)
我已经插入了一些调试输出,但看起来一切正常:(
有人能帮我吗/
最重要的是,
var Object=
不要这样做:)有两件事你可以试试。能否使用本机mongo驱动程序保存/更新?这可能表明您是否具有正确的权限(因为您似乎能够阅读)。您的对象是否已保存(尽管没有触发回调)?@AndrewLavers已更改,谢谢:)@Edo否文档将不会保存,但可能会发生验证错误。我已经试过了。找到它,它就工作了。回调将被触发。但储蓄是行不通的。事实上,我必须拥有完全的权限(它在其他应用程序中以同样的方式工作)这取决于您,但如果我处于您的位置,我会测试是否能够使用本机驱动程序进行编写。由于您似乎已正确设置了数据库连接。。。
module.exports = global.db.model('Object', dbObjectSchema);
var newObject = new global.models.Object({});
newObject.customerId = data.customerId;
newObject.customerPrefix = data.prefix;
newObject.fields = data.fields;
newObject.name = data.name;
console.log(global.db.connection.host); // localhost
console.log(global.db.connection.port); // 27017
// i've tested also whether i can call .find (it works and callback will fired!)
newObject.save(function(err) {
// this part will never called
console.log("save callback fired");
if (typeof callback == 'function') {
callback(err, newObject);
}
});