ValidationError: Cast to [undefined] failed

ValidationError: Cast to [undefined] failed

我有两个要连接的猫鼬模型 GameCard。我关注 this youtube tutorial 并且能够 CRUD games,但是当我想将卡片添加到卡片数组时,出现以下错误:

ValidationError: game validation failed: cards: Cast to [undefined] failed for value "[{"_id":"5a65b32e8f4af32fb8626db3","game":"5a651a319aa3c22d6c9ebeb7","__v":0}]" at path "cards"
    at new ValidationError (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/error/validation.js:27:11)
    at model.Document.invalidate (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:1656:32)
    at _init (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:418:18)
    at init (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:386:5)
    at model.Document.$__init (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:356:3)
    at model.syncWrapper [as $__init] (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/kareem/index.js:234:23)
    at model.Document.init (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:324:8)
    at completeOne (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/query.js:1988:12)
    at Immediate.<anonymous> (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/query.js:1508:11)
    at Immediate.<anonymous> (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mquery/lib/utils.js:119:16)
    at runCallback (timers.js:756:18)
    at tryOnImmediate (timers.js:717:5)
    at processImmediate [as _immediateCallback] (timers.js:697:5)

此外,当我尝试使用 postman POST Body/raw/JSONpost 将新卡 post 加入游戏时,不时出现第二个错误( application/JSON):

RangeError: Maximum call stack size exceeded
    at model.Document.toJSON (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:2356:37)
    at clone (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:164:18)
    at cloneArray (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:260:14)
    at clone (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:159:12)
    at cloneObject (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:246:11)
    at clone (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:172:16)
    at model.Document.$toObject (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:2065:13)
    at model.Document.toJSON (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:2357:15)
    at clone (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:164:18)
    at cloneObject (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:246:11)
    at clone (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:172:16)
    at model.Document.$toObject (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:2065:13)
    at model.Document.toJSON (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/document.js:2357:15)
    at clone (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:164:18)
    at cloneArray (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:260:14)
    at clone (/mnt/c/Users/Martin/Documents/Web/card-creator/node_modules/mongoose/lib/utils.js:159:12)

我注意到,还有其他人遇到此错误,但据我所知,模型配置错误,但我在这里找不到错误。

server/models/cardModel.js

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

const cardSchema = new Schema({
    description: String,
    flavor: String,
    game: {
        type: Schema.Types.ObjectId,
        ref: 'Game'
    }
});

const Card = mongoose.model('Card', cardSchema);
module.exports = Card;

server/models/gameModel.js

const Card = require('../models/cardModel');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const gameSchema = new Schema({
    title: String,
    description : String,
    cards: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Card'
        }
    ]
});

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

server/routes/games.js

const express = require('express');
// const router = express.Router();
const router = require('express-promise-router')();
const Card = require('../models/cardModel');
const Game = require('../models/gameModel');

router.route('/:gameId/cards')
  .post(async (req, res, next) => {
    var { gameId } = req.params;
    var newCard = new Card(req.body);
    console.log('newCard', newCard);
    var game = await Game.findById(gameId);
    // Assign card to game
    newCard.game = game;
    await newCard.save();
    // Add card to game's card array
    game.cards.push(newCard);
    await game.save();
    res.status(201).json(newCard);
});

我在 /:gameId/cards 的 post 函数中发现错误:newCard 被推送到数组,但由于这是一个异步函数,保存仍然需要等待 (await ) 才能做到这一点。

错误:

...
await newCard.save();
game.cards.push(newCard);
await game.save()
res.status(201).json(newCard);

右:

...
await newCard.save();
await game.cards.push(newCard);
await game.save()
res.status(201).json(newCard);