带有对象的 Mongoose 模式

Mongoose schema with object

我尝试用 NodeJS 在 MongoDB 中填写文档,因此我创建了一个架构和一个 post 请求。

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

所以由于请求,我想填充此架构。

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;

但是,当我 运行 它有一条错误消息 "TypeError: Cannot read property 'name' of undefined" 但是 我不明白为什么因为 developer.name 存在。

我猜错误不是指 game.developer.name 而是指 req.body.developer.name.
尝试将您的行更改为

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

因为您的参数 developer.name 被解析为字符串,而不是嵌套对象。