我有一个节点应用程序,当我创建帖子时,我必须传递创建帖子ID的人以及帖子。问题是我总是这样写所有东西
myUrl
我现在的问题是,如果我有很多这样的东西,是否不可能使用像lodash这样的库来选择所有类似
的字段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
});
每次我像这样尝试时,我都会得到404,而在终端中,我将会得到
消息:“路径
const body = _.pick(req.body, ['produtTitle', 'productImg', 'productPrice', 'productDesc']) console.log(`THIS IS ${body}`) const product = new Product({ body, userId: req.userId });
是必需的。”, 名称:“ ValidatorError”, 属性:[对象], 种类:“必填”, 路径:“ productPrice”, 值:未定义, 原因:未定义, '$ isValidatorError':true},
所有字段
如果我像这样productPrice
这样做,我只会收到404错误
const product = new Product(req.body)
我正在看一个图书馆,因为把所有内容写出来很繁琐,并且在需要大量字段的情况下
答案 0 :(得分:0)
您可以使用lodash,但在创建新产品时将body作为关键。您需要散开身体的按键。
const body = _.pick(req.body, ['produtTitle', 'productImg', 'productPrice', 'productDesc'])
const product = new Product({
...body,
userId: req.userId
});