我有一个节点应用程序,我创建了一个post,我必须传递创建post ID和post的人。问题是我总是像这样把所有的东西都写出来
const imageUrl = req.file.path;
const title = req.body.title;
const content = req.body.content;
const post = new Post({
title: title,
content: content,
imageUrl: imageUrl,
creator: req.userId
});我现在的问题是,如果我有很多这样的东西,难道就不可能用一个库,比如lodash,来挑选出所有的领域。
const body = _.pick(req.body, ['produtTitle', 'productImg', 'productPrice', 'productDesc'])
console.log(`THIS IS ${body}`)
const product = new Product({
body,
userId: req.userId
});每次我这样尝试,我都会得到404,而在我的冒险中,我得到了
消息:“需要路径
productPrice。”,名称:“ValidatorError”,属性:对象,种类:“必需”,路径:“产品价格”,值:未定义,原因:未定义,“$isValidatorError”:true },
为所有的领域
如果我像这样做const product = new Product(req.body),我只得到一个错误的404
message: 'Path `userId` is required.',
name: 'ValidatorError',
properties: [Object],
kind: 'required',
path: 'userId',
value: undefined,
reason: undefined,
'$isValidatorError': true } }我看的是一个库,因为把所有的东西都写出来可能非常乏味,而且在需要大量字段的情况下。
发布于 2019-05-05 12:51:16
您可以使用存档,但您是通过身体作为一个关键时,创造新的产品。你得把身体的钥匙摊开。
const body = _.pick(req.body, ['produtTitle', 'productImg', 'productPrice', 'productDesc'])
const product = new Product({
...body,
userId: req.userId
});https://stackoverflow.com/questions/55991972
复制相似问题