首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将userId添加到post请求节点表达式

将userId添加到post请求节点表达式
EN

Stack Overflow用户
提问于 2019-05-05 12:31:54
回答 1查看 997关注 0票数 0

我有一个节点应用程序,我创建了一个post,我必须传递创建post ID和post的人。问题是我总是像这样把所有的东西都写出来

代码语言:javascript
复制
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,来挑选出所有的领域。

代码语言:javascript
复制
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

代码语言:javascript
复制
message: 'Path `userId` is required.',
        name: 'ValidatorError',
        properties: [Object],
        kind: 'required',
        path: 'userId',
        value: undefined,
        reason: undefined,
        '$isValidatorError': true } }

我看的是一个库,因为把所有的东西都写出来可能非常乏味,而且在需要大量字段的情况下。

EN

Stack Overflow用户

回答已采纳

发布于 2019-05-05 12:51:16

您可以使用存档,但您是通过身体作为一个关键时,创造新的产品。你得把身体的钥匙摊开。

代码语言:javascript
复制
const body = _.pick(req.body, ['produtTitle', 'productImg', 'productPrice', 'productDesc'])

    const product = new Product({
      ...body,
      userId: req.userId
    });
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55991972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档