繁体   English   中英

如何避免 TypeError: callback.apply is not a function and Callback was already called ? 节点js

[英]How to avoid TypeError: callback.apply is not a function and Callback was already called ? node js

我得到 ++TypeError: callback.apply is not a function 并且 ++Callback 已经被调用。

这是我的代码:

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

谢谢你帮助我。

您的代码中有很多错误

  1. CarModel更新方法中查询和更新顺序不匹配,猫鼬update有4个参数

    Model.update(conditions, update, options, callback)

  2. .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 () { 

  }
  );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM