简体   繁体   中英

mongodb array data store using node js

I have to try to store my child info into MongoDB via using postman tool. But it shows me this message "message": "child info validation failed" in postman console. child info is my collection name where I store my child info.

my requirement is to store the result in array form as below schema mentioned inside MongoDB

1). This is js child schema

   userId:{
            type: mongoose.Schema.Types.ObjectId,
          ref: 'User'
          },
   quiz:[
        {
        questionId:{
                type: mongoose.Schema.Types.ObjectId,
                    ref: 'questions'
                    },
        score:{type:String},
        time:{type:String}
        }
      ]

2). This is node js

    try {

            var quizArr = [];
            var quizObj = {
                'questionId': req.params.questionId,
                'score': req.params.score,
                'time': new Date().toISOString()
            };
            quizArr.push(quizObj);
            var userObj = {
                'userid': req.params.userId,
                'quiz': quizArr
            };
            //var parseObj = Json.stringify(userObj);  This line is comment

            var childinfoSave = new QuizChildInfo(userObj);

            childinfoSave.save(function (err) {
                if (err) return next(err);
                res.send("Child questionId score and date saved successfully")
                console.log("Child questionId score and date saved successfully");
            });
      }catch(err){console.log(err);}

3). Output of postman screen

 {
  "message": "childinfos validation failed"
 }

4). Output of console

    Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

5). Mongo console

     {
     "_id" : ObjectId("57bc483169e718642ac0ac44"),
     "levelsAttempted" : [ ],
     "quiz" : [ ],
     "__v" : 0
     }

For the problem of your console,

put mongoose.Promise = global.Promise; in the file where you have established your server connection( like in my case its index.js ).

And i think you may have not posted the whole code as i couldnt find childinfos in your code.

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