简体   繁体   English

猫鼬架构与对象

[英]Mongoose schema with object

I try to fill a document in MongoDB with NodeJS, consequently I created a Schema and a post req. 我尝试使用NodeJS在MongoDB中填充文档,因此我创建了一个Schema和一个post req。

var gameSchema = new Schema({
       title: String,
        developer: {
            name: String,
            email: String
        },
        isBroadcasted: Boolean
    });

So I want to populate this schema thanks to a req. 因此,由于需要,我想填充此架构。

router.post('/android', auth, function(req, res){
    // Create a new instance of the Game model
    var game = new Game();
        game.title = req.body.title;

game.developer.name = req.body.developer.name;
game.developer.email = req.body.developer.email;

在此处输入图片说明

But, when I run it there is an error message "TypeError: Cannot read property 'name' of undefined" but I don't understand why because developer.name exists. 但是,当我运行它时,出现一条错误消息“ TypeError:无法读取未定义的属性'name'”,但我不明白为什么,因为存在developer.name。

I guess the error is not referring to game.developer.name but to req.body.developer.name . 我猜该错误不是指game.developer.name而是req.body.developer.name
Try changing your line to 尝试将行更改为

game.developer.name = req.body['developer.name']

as your parameter developer.name is parsed as string, not as nested object. 因为您的参数developer.name被解析为字符串,而不是嵌套对象。

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

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