简体   繁体   中英

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

I got ++TypeError: callback.apply is not a function and ++Callback was already called.

this is my code:

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 () {}
  );

};

this is carModel schema.

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});

What should I do please?

and this the error in terminal:

[

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

thanks for helping me.

There are a lot of Errors in your code

  1. There is mismatch of the query and update orders in CarModel update method, the mongoose update have 4 parameters

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

  2. Inside One function in .series array you are performing two Other async function with callbacks, which will definitely throw an error

Callback was already Called

Look at this Code Below and use

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

  }
  );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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