简体   繁体   English

使用节点js的mongodb数组数据存储

[英]mongodb array data store using node js

I have to try to store my child info into MongoDB via using postman tool. 我必须尝试使用​​邮递员工具将我的孩子信息存储到MongoDB中。 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 我的要求是将结果存储在MongoDB中提到的以下架构的数组形式中

1). 1)。 This is js child schema 这是js子架构

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

2). 2)。 This is node js 这是节点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). 3)。 Output of postman screen 邮递员屏幕输出

 {
  "message": "childinfos validation failed"
 }

4). 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). 5)。 Mongo console Mongo控制台

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

For the problem of your console, 对于您的控制台问题,

put mongoose.Promise = global.Promise; mongoose.Promise = global.Promise;放到mongoose.Promise = global.Promise; in the file where you have established your server connection( like in my case its index.js ). 在您已建立服务器连接的文件中(例如,在本例中为index.js )。

And i think you may have not posted the whole code as i couldnt find childinfos in your code. 而且我认为您可能尚未发布整个代码,因为我无法在您的代码中找到childinfos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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