简体   繁体   English

当将我的Mongoose数据库模式的实例放置在另一个模式中时,为什么会“广播到未定义”?

[英]Why might one of my Mongoose DB schemas be being “cast to undefined” when instances of it are placed in a second schema?

I have created a Mongoose DB schema called Game, and a second called Player which contains an array of said Game schema objects. 我创建了一个名为Game的Mongoose DB模式,并创建了一个名为Player的第二个模式,其中包含上述Game模式对象的数组。 When I run the line player.games = [ game ]; 当我运行line player.games = [game]; shown in the app.js snippet below, I get the following error: 如下面的app.js片段所示,出现以下错误:

Cast to undefined failed for value “{ gameID: 551175ee7a81c2c10c000002, points: 10 }” at path “games”

What am I doing wrong, please? 请问我做错了什么?

app/models/Game.js (entire file): app / models / Game.js(整个文件):

var mongoose = require( "mongoose" );
var Schema   = mongoose.Schema;

var GameSchema = new Schema({
    _id :    false,
    id :     false,
    gameID : Schema.ObjectId,
    points : Number
});

module.exports = mongoose.model( "Game", GameSchema );

app/models/Player.js (entire file): app / models / Player.js(整个文件):

var mongoose = require( "mongoose" );
var Game     = require( "../models/game" );
var Schema   = mongoose.Schema;

var PlayerSchema = new Schema({
    socialID : Number,
    games :    [ Game ] 
});

module.exports = mongoose.model( 'Player', PlayerSchema );

app.js (snippet): app.js(摘要):

router.route( '/players' ).post( function( req, res )
{
    var game = new Game();
    game.gameID = req.body.gameID;
    game.points = req.body.points;

    var player = new Player();
    player.socialID = req.body.socialID;
    player.games = [ game ];

    player.save(... etc.

chridam, thank you. chridam,谢谢。 The solution you suggested did not work,^ but by making some small adjustments to it I made it work. 您建议的解决方案不起作用,^但对其进行了一些小调整,就使它起作用。 I wonder whether you could suggest whether my small adjustments are an acceptable solution, or a nasty fudge, or even some third etc. option please? 我想知道您是否可以建议我的小调整是可以接受的解决方案,还是令人讨厌的软糖,甚至是第三种等等选择?

^ was getting "TypeError: object is not a function" at "var game = new Game()" ^在“ var game = new Game()”处获得“ TypeError:object is not a function”

The adjustment was basically to add a Wrapper model for game.js 所做的调整基本上是为game.js添加包装器模型

Edited game.js: 编辑的game.js:

var mongoose     = require( 'mongoose' );
var Schema       = mongoose.Schema;

var Schema = new Schema({
    _id :    false,
    id :     false,
    gameID : Schema.ObjectId,
    points : Number
});

module.exports = mongoose.model( 'Game', Schema );

Additional game_wrapper.js 其他game_wrapper.js

var Game = require( '../models/game' );
module.exports = Game.Schema;

Edited player.js: 编辑的player.js:

var mongoose    = require( 'mongoose' );
var GameWrapper = require( '../models/game_wrapper' );
var Schema      = mongoose.Schema;

var Schema = new Schema({
    socialID : Number,
    games :    [ GameWrapper ]
});

module.exports = mongoose.model( 'Player', Schema );

Edited app.js: 编辑的app.js:

var Player = require( './app/models/player' );
var Game   = require( './app/models/game' );

router.route( '/players' ).post( function( req, res )
{
    var game = new Game({
        gameID: req.body.gameID,
        points: req.body.points
    });

    var player = new Player({
        socialID: req.body.socialID,
        games: [ game ]
    });

    player.save(...)
}

You are passing a Game model into the parent schema definition PlayerSchema rather than a GameSchema schema object. 您正在将Game模型传递到父模式定义PlayerSchema而不是GameSchema模式对象。 In your GameSchema module app/models/Game.js, change it to: 在您的GameSchema模块app / models / Game.js中,将其更改为:

var mongoose = require( "mongoose" );
var Schema   = mongoose.Schema;

var GameSchema = new Schema({
    _id :    false,
    id :     false,
    gameID : Schema.ObjectId,
    points : Number
});

module.exports = GameSchema;

Then change your PlayerSchema definition to: 然后将您的PlayerSchema定义更改为:

var mongoose = require( "mongoose" );
var GameSchema     = require( "../models/game" );
var Schema   = mongoose.Schema;

var PlayerSchema = new Schema({
    socialID: Number,
    games: [GameSchema] 
});

module.exports = mongoose.model( 'Player', PlayerSchema );

In your route, wrap up your request object into the schema: 在路由中,将请求对象包装到架构中:

router.route( '/players' ).post( function( req, res )
{
    var game = new Game({
        gameID: req.body.gameID,
        points: req.body.points
    });

    var player = new Player({
        socialID: req.body.socialID,
        games: [game]
    });

    player.save(...)
}

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

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