typeError CB不是一个功能node.js

时间:2020-04-09 04:54:56

标签: node.js

我目前正在关注关于express和node.js https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose的教程,但是我对代码进行了一些修改以确保我理解该教程。我目前正在尝试通过mongoose将数据添加到mongoDB,并创建了一个脚本来添加示例数据,但是当我运行它时出现错误

C:\***\node_modules\mongoose\lib\helpers\promiseOrCallback.js:19
            throw error;
            ^

TypeError: cb is not a function

我该如何解决

我的代码是:

#! /usr/bin/env node

console.log("this script populates some test details into my database");

var userArgs = process.argv.slice(2);

var async = require('async')
var pic = require('./mongo/pic')
var genre = require('./mongo/genre')

var mongoose = require('mongoose');
var mongoDB = userArgs[0];
mongoose.connect(mongoDB, { useNewUrlParser: true });
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

var pics = []
var genres = []

function genreCreate(name, cb) {
    genredetail = {name:name}

    var Genre = new genre(genredetail);

    Genre.save(function (err) {
      if (err) {
        cb(err, null)
        return
      }
      console.log('New genre: ' + Genre);
      genres.push(Genre)
      cb(null, Genre)
    }  );
}

function picCreate(image, source, genre, cb) {

    picDetail = {image:image, source:source, genre:genre}

    var Pic = new pic(picDetail);

    Pic.save(function (err) {
      if (err) {
        cb(err, null);
        return;
      }
      console.log('New Pic: ' + image);
      pics.push(Pic)
      cb(null, Pic);
    });
}

function  createGenres(cb){
  async.series([
        function(callback) {
          genreCreate("space", callback);
        },
        function(callback) {
            genreCreate("e", callback);
        },
        function(callback) {
            genreCreate("pizza", callback);
        },
        function(callback) {
            genreCreate("cone", callback);
        },
        function(callback) {
            genreCreate("winrar", callback);
        },
    ], cb);
}
function createPics(cb){
    async.parallel([
        function(callback) {
            picCreate("moon", "the moon", [genres[0],], callback);
        },
        function(callback) {
            picCreate("Hub", "test", [genres[1],], callback);
        },
        function(callback) {
            picCreate("example", "pc", [genres[0],genres[1],genres[2]], callback);
        },
        function(callback) {
            picCreate("cone.rar", "instagram", [genres[3],genres[4]], callback);
        },
        function(callback) {
            picCreate("void", "nowhere", false, callback);
        }
    ], cb)
}

async.series([
    genreCreate,
    createPics
],
function(err, results){
    if (err){
        console.log('FINAL ERR: '+err);
    } else {
        console.log('Pics: '+pics);
    }
    mongoose.connection.close();
});

0 个答案:

没有答案