Mongoose doesn't save data to the MongoDB

前端 未结 3 885
情歌与酒
情歌与酒 2021-02-04 18:02


Below is an object literal I am trying to save to MongoDB. It is defined within the app.js file which is an Express server. As the object is hardcoded within the server, my

3条回答
  •  再見小時候
    2021-02-04 18:33

    I found a few problems when trying to reproduce this locally.

    You're not calling next() in newsSchema.pre('save')

    Should be

    newsSchema.pre('save', function(next){
        if( !this.addedOn ) this.addedOn = new Date();
        if( !this.addedBy ) this.addedBy = adminUser;
        next();
    });
    

    You also need to make sure that you are connected to the db before doing any of this stuff, not sure if you are or not since i didn't see that part of the code.

提交回复
热议问题