【发布时间】:2017-07-25 19:19:00
【问题描述】:
我正在构建一个以 mongo 作为后端的平均堆栈应用程序。 我的猫鼬模型中有一个教堂模式,如下所示:
{
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');
var sequenceGenerator = require('mongoose-sequence-plugin');
var unit = new Schema({
unit_id: {type: String},
unit_name: {type: String, unique: true},
unit_address: {type: String}
});
var schema = new Schema({
church_id: {type: String},
church_name: {type: String, required: true, unique: true},
email: {type: String, required: true, unique: true},
parishname: {type: String},
diocese: {type: String},
units: [unit],
church_reg_id: {type: String, required: true, unique: true},
address: {type: String},
city: {type: String},
zipcode: {type: Number},
state: {type: String},
church_contact_no: {type: Number}
});
schema.plugin(mongooseUniqueValidator);
schema.plugin(sequenceGenerator, {
field: 'church_id',
startAt: '0001',
prefix: 'CHURCH_',
maxSaveRetries: 3
});
module.exports = mongoose.model('Church', schema);
现在我正在尝试更新我当前架构中的单位子文档,该架构已经包含其他字段的数据。更新代码如下:
router.patch('/addunit/:uname/:uaddress', function (req, res, next) {
console.log(req.params.uname);
Church.findOne({church_id: req.body.church_id}, function (err, church) {
if (err) {
return res.status(500).json({
title: 'No Church Details Found',
error: err
});
}
console.log(church.church_reg_id);
console.log(church.church_name);
church.units.push({
unit_name: req.params.uname,
unit_address: req.params.uaddress,
unit_id: req.body.church_id + "Unit_" + req.params.uname
});
console.log(church.units);
church.save(function (err, result) {
if (err) {
return res.status(500).json({
title: 'An error occured',
error: err
});
}
res.status(201).json({
message: 'Unit Added',
obj: result
})
});
});
});
现在的问题是代码运行成功,并且单位字段在后端成功更新,但此后出现以下错误。
D:\Church\node_modules\mongodb\lib\utils.js:123
process.nextTick(function() { throw err; });
^
TypeError: callback is not a function
at D:\Church\node_modules\mongoose\lib\model.js:267:5
at D:\Church\node_modules\mongoose\lib\model.js:195:9
at handleCallback (D:\Church\node_modules\mongodb\lib\utils.js:120:56)
at D:\Church\node_modules\mongodb\lib\collection.js:1062:5
at D:\Church\node_modules\mongodb-core\lib\connection\pool.js:469:18
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
请帮助查明问题。
【问题讨论】:
-
代码似乎没有错误,我认为问题出在不同的代码文件中。
-
@JiteshTukadiya 感谢您的回复。实际上我的 app.js 文件中有一行 mongoose.Promise = global.Promise;当我注释掉该行时,出现以下错误:- (node:6480) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, 请插入您自己的 promise 库:mongoosejs.com/docs/promises.html D:\Church\node_modules\ mongodb\lib\utils.js:123 process.nextTick(function() { throw err; }); ^ TypeError: callback is not a function 请建议。
标签: node.js mongoose mongoose-schema