我得到了++ TypeError:callback.apply不是一个函数 和++回调已被调用。
这是我的代码:
var toFix = {
'Mercedes': 'Mercedes-Benz'
};
var toUpdate = {
'A-CLASSE': 'Classe A',
'CLASSE A': 'Classe A',
};
async.series([
function (cb) {
console.log('Clean cars...');
async.forEachOfSeries(toFix, function (to, from, cb) {
console.log(`FixMakeForCars ${from} -> ${to}`);
Car.update({'make': {$regex: S(from).trim().s, $options: 'i'}}, {$set: {'make': to}}, {multi: true}, cb);
}, cb);
async.forEachOfSeries(toUpdate, function (to, from, cb) {
console.log(`UpdateModelForCars ${from} -> ${to}`);
Car.update({'model': {$regex: S(from).trim().s, $options: 'i'}}, {$set: {'model': to}}, {multi: true}, cb);
}, cb);
},
function (cb) {
console.log('Clean car models...');
async.forEachOfSeries(toFix, function (to, from, cb) {
console.log(`FixCarModelMake ${from} -> ${to}`);
CarModel.update(
{'make': {$exists: true}}, {'model': {$exists: true}}, {'year': {$exists: true}},
{'make': {$regex: S(from).trim().s, $options: 'i'}}, {$set: {'make': to}}, {multi: true}, cb);
}, cb);
async.forEachOfSeries(toUpdate, function (to, from, cb) {
console.log(`UpdateModel ${from} -> ${to}`);
CarModel.update(
{'make':{ $exists: true}}, {'model':{ $exists: true}}, {'year':{ $exists: true}},
{'car.model': {$regex: S(from).trim().s, $options: 'i'}}, {$set: {'car.model': to}}, {multi: true}, cb);
}, cb);
},
], function () {}
);
};
这是carModel架构。
var schema = new mongoose.Schema({
make: {type: String, trim: true, required: true},
model: {type: String, trim: true, required: true},
year: {type: Number},
enabled: {type: Boolean, default: false},
range: {type: String, enum: _.values(Range)},
premium: {type: Boolean, default: false},
picture: {},
status: {type: String, enum: _.values(CarModelStatus), default: CarModelStatus.DRAFT}
});
schema.index({model: 1, make: 1, year: 1}, {unique: true});
我该怎么办?
这是终端中的错误:
[
2017-03-31T12:29:15.219Z] DEBUG: job/306 on f2a8df0485ee: FixCarModelMake Mercedes -> Mercedes-Benz (env=development)
caller: {
"line": "269",
"pos": "17",
"file": "job/apply-updates/3.8.1-update-to-Mercedes-Benz.js"
}
[2017-03-31T12:29:15.237Z] ERROR: job/306 on f2a8df0485ee: (env=development)
TypeError: callback.apply is not a function
at /sources/node_modules/mongoose/lib/model.js:3411:16
谢谢你的帮助。
答案 0 :(得分:1)
您的代码中存在很多错误
CarModel
更新方法中的查询和更新订单不匹配,猫鼬update
有4个参数
Model.update(conditions, update, options, callback)
在.series
数组中的一个函数中,您正在使用回调执行两个其他async
函数,这肯定会引发错误
Callback was already Called
请查看以下代码并使用
async.series([
function (cb) {
console.log('Clean cars...');
// .eachOfSeries
async.eachOfSeries(toFix, function (to, from, cb) {
console.log(`FixMakeForCars ${from} -> ${to}`);
Car.update({ 'make': { $regex: S(from).trim().s, $options: 'i' } }, { $set: { 'make': to } }, { multi: true }, function (err, res) {
if (err) return cb(err);
cb();
});
}, cb);
},
function (cb) {
async.eachOfSeries(toUpdate, function (to, from, cb) {
console.log(`UpdateModelForCars ${from} -> ${to}`);
Car.update({ 'model': { $regex: S(from).trim().s, $options: 'i' } }, { $set: { 'model': to } }, { multi: true }, function (err, res) {
if (err) return cb(err);
cb()
});
}, cb);
},
function (cb) {
console.log('Clean car models...');
async.eachOfSeries(toFix, function (to, from, cb) {
console.log(`FixCarModelMake ${from} -> ${to}`);
// formed the valid query, no mismatch of orders
let query = {
'make': { $exists: true },
'model': { $exists: true },
'year': { $exists: true },
'make': { $regex: S(from).trim().s, $options: 'i' }
}
CarModel.update(query, { $set: { 'make': to } }, { multi: true } , function (err, res) {
if (err) return cb(err);
cb()
})
}, cb);
},
function (cb) {
async.eachOfSeries(toUpdate, function (to, from, cb) {
console.log(`UpdateModel ${from} -> ${to}`);
// formed the valid query, no mismatch of orders
var query = {
'make': { $exists: true },
'model': { $exists: true },
'year': { $exists: true },
'model': { $regex: S(from).trim().s, $options: 'i' }
}
CarModel.update(query, { $set: { 'model': to } }, { multi: true } , function (err, res) {
if (err) return cb(err);
cb()
})
}, cb );
}
], function () {
}
);