ValidationError:转换为[undefined]失败

时间:2018-01-22 10:39:25

标签: json mongodb mongoose mongoose-schema

我想连接两个猫鼬模型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 / JSON(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)

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

  • 节点:9.4.0
  • 猫鼬:5.0.1
  • MongoDB:3.6.2

服务器/模型/ 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;

服务器/模型/ 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;

服务器/路由/ 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);
});

1 个答案:

答案 0 :(得分:0)

我在/: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);