ValidationError:转换为 [undefined] 失败
Posted
技术标签:
【中文标题】ValidationError:转换为 [undefined] 失败【英文标题】:ValidationError: Cast to [undefined] failed 【发布时间】:2018-06-30 23:14:35 【问题描述】:我有两个想要连接的猫鼬模型 Game
和 Card
。我关注了this youtube tutorial,我可以对games
进行CRUD,但是当我想将卡片添加到卡片数组中时,出现以下错误:
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.2server/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);
);
【问题讨论】:
【参考方案1】:我在/: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);
【讨论】:
你完全错了兄弟... console.log(typeof game.cards.push(newCard)) // 打印'number'以上是关于ValidationError:转换为 [undefined] 失败的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Django 不将我的 unique_together 约束强制为 form.ValidationError 而不是抛出异常?