节点/ Mongoose错误:ValidationError

时间:2016-12-25 13:19:49

标签: javascript node.js mongodb

这是我的server.js。当我运行node server.js然后使用PostMan发布json时,它会给我以下错误。

(node:6863) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
ValidationError: Post validation failed
    at MongooseError.ValidationError (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/error/validation.js:23:11)
    at model.Document.invalidate (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/document.js:1486:32)
    at /Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/document.js:1362:17
    at validate (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/schematype.js:705:7)
    at /Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/schematype.js:742:9
    at Array.forEach (native)
    at SchemaString.SchemaType.doValidate (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/schematype.js:710:19)
    at /Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/document.js:1360:9
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

错误:

$reponse = $bdd->exec( "insert into client(cin,nom,mot_de_passe) values ($cin,'$nom','$mot_de_passe)'" );

我正在尝试通过“使用MEAN堆栈编写现代Web应用程序”来学习MEAN堆栈,但我一直在遇到问题,即使我完全遵循代码和指令。任何人都可以帮助理解这个错误,并可能推荐一些很好的资源来学习平均堆栈吗?

1 个答案:

答案 0 :(得分:1)

由于您提供了mongoose validation,因此会触发此错误 您的架构(在/models/post中)并且验证失败。

例如,如果你提供了这样的模型:

var postSchema = new Schema({

    "username": String,
    "body": String,
    "email": {
        type: String,
        required: true
    }

});
var Post = mongoose.model('Post', postSchema);

这会失败,因为email required验证程序不受尊重。查找验证工具here的完整列表。

旁注:res.sendStatus(201).json(post)将在发送201状态的响应后设置json正文和内容类型标题。要发送两个使用:

res.status(201).json(post)