MongoError:未知修饰符:$ pushAll ...如何编辑我的代码?

时间:2019-10-11 13:28:18

标签: node.js mongodb push

这是我的index.js文件:

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

const Dishes = require('./models/dishes');

const url = 'mongodb://localhost:27017/conFusion';
const connect = mongoose.connect(url, {
    useMongoClient: true
});

connect.then((db) => {

    console.log('Connected correctly to server');

    Dishes.create({
        name: 'Uthappizza',
        description: 'test'
    })
    .then((dish) => {
        console.log(dish);

        return Dishes.findByIdAndUpdate(dish._id, {
            $set: { description: 'Updated test'}
        },{ 
            new: true 
        })
        .exec();
    })
    .then((dish) => {
        console.log(dish);

        dish.comments.push({
            rating: 5,
            comment: 'I\'m getting a sinking feeling!',
            author: 'Leonardo di Carpaccio'
        });

        return dish.save();
    })
    .then((dish) => {
        console.log(dish);

        return db.collection('dishes').drop();
    })
    .then(() => {
        return db.close();
    })
    .catch((err) => {
        console.log(err);
    });
});

这是dishes.js文件:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

var commentSchema = new Schema({
    rating:  {
        type: Number,
        min: 1,
        max: 5,
        required: true
    },
    comment:  {
        type: String,
        required: true
    },
    author:  {
        type: String,
        required: true
    }
}, {
    timestamps: true
});

var dishSchema = new Schema({
    name: {
        type: String,
        required: true,
        unique: true
    },
    description: {
        type: String,
        required: true
    },
    comments:[commentSchema]
}, {
    timestamps: true
});

var Dishes = mongoose.model('Dish', dishSchema);

module.exports = Dishes;

但是出现以下错误:

> node-mongoose@1.0.0 start C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose
> node index

Connected correctly to server
{ __v: 0,
  updatedAt: 2019-10-11T13:22:32.527Z,
  createdAt: 2019-10-11T13:22:32.527Z,
  name: 'Uthappizza',
  description: 'test',
  _id: 5da082181d223831f447b6a1,
  comments: [] }
(node:12788) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
{ _id: 5da082181d223831f447b6a1,
  updatedAt: 2019-10-11T13:22:32.543Z,
  createdAt: 2019-10-11T13:22:32.527Z,
  name: 'Uthappizza',
  description: 'Updated test',
  __v: 0,
  comments: [] }
{ MongoError: Unknown modifier: $pushAll
    at Function.MongoError.create (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\error.js:31:11)
    at toError (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb\lib\utils.js:139:22)
    at C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb\lib\collection.js:1059:67
    at C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\connection\pool.js:469:18
    at process._tickCallback (internal/process/next_tick.js:61:11)
  name: 'MongoError',
  message: 'Unknown modifier: $pushAll',
  driver: true,
  index: 0,
  code: 9,
  errmsg: 'Unknown modifier: $pushAll' }
Unhandled rejection MongoError: E11000 duplicate key error collection: conFusion.dishes index: name_1 dup key: { : "Vadonut" }
    at Function.MongoError.create (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\error.js:31:11)
    at C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\connection\pool.js:497:72
    at authenticateStragglers (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\connection\pool.js:443:16)
    at Connection.messageHandler (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\connection\pool.js:477:5)
    at Socket.<anonymous> (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\connection\connection.js:331:22)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

我是NodeJS和MongoDB的新学习者,并且从教程视频中看到了此代码。我知道push方法已过时,但我不知道如何用新方法替换它来解决问题?我试图用concat方法替换它,但似乎出现错误。

编辑:我试图替换代码的以下行:

dish.comments.push([{
    rating: 5,
    comment: 'I\'m getting a sinking feeling!',
    author: 'Leonardo di Carpaccio'
}]);

此行:

dish = dish.comments.concat([{
    rating: 5,
    comment: 'I\'m getting a sinking feeling!',
    author: 'Leonardo di Carpaccio'
}]);

但仍然出现此错误:

> node-mongoose@1.0.0 start C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose
> node index

Connected correctly to server
{ __v: 0,
  updatedAt: 2019-10-11T13:40:55.533Z,
  createdAt: 2019-10-11T13:40:55.533Z,
  name: 'Uthappizza',
  description: 'test',
  _id: 5da08667104e154a9c6cb0a1,
  comments: [] }
(node:19100) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
{ _id: 5da08667104e154a9c6cb0a1,
  updatedAt: 2019-10-11T13:40:55.611Z,
  createdAt: 2019-10-11T13:40:55.533Z,
  name: 'Uthappizza',
  description: 'Updated test',
  __v: 0,
  comments: [] }
TypeError: dish.save is not a function
    at Dishes.create.then.then (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\index.js:38:21)
    at tryCatcher (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
Unhandled rejection MongoError: E11000 duplicate key error collection: conFusion.dishes index: name_1 dup key: { : "Vadonut" }
    at Function.MongoError.create (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\error.js:31:11)
    at C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\connection\pool.js:497:72
    at authenticateStragglers (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\connection\pool.js:443:16)
    at Connection.messageHandler (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\connection\pool.js:477:5)
    at Socket.<anonymous> (C:\Users\m\Desktop\JS\NodeJS\Server-side-Development-with-NodeJS-Express-and-MongoDB-master\node-mongoose\node_modules\mongodb-core\lib\connection\connection.js:331:22)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

0 个答案:

没有答案