使用POSTMAN发布数据后未获取数据

阿西特·辛格

这是我的nodejs代码,在这里我逐步注释掉我对代码所做的事情,现在我将使用POSTMAN发布数据

   exports.votes = async (req, res, next) => {
  try {
    /**
     * 1. get the poll from db
     * 2. check if the user already exists in any option
     * 3. if user has already selected any option do nothing
     * 4. if user has selected any other option remove from that option
     * 5. if user does not exist in any option, insert his user id to selected option
     */
    const { pollId } = req.params;
    console.log(pollId);
    let { userId, answer } = req.body;
    console.log(userId);
    console.log(answer);
    // get selected poll from db
    const poll = await Poll.findByIdAndUpdate(pollId, {
  $set: { options: poll.options },
}, {new: true});
    if (answer && poll) {
      answer = answer.toLowerCase();
      ///Finf the Poll

      let existingVote = null;
      Object.keys(poll.options).forEach((option) => {
        // loop on all options, check if the user already exists in any option
        if (poll.options[option].indexOf(userId) !== -1) {
          existingVote = option;
  
        }
      });
      if (existingVote == null) {
        // if there is no existing vote save it to db
        try {
          const push = {};
          push[`options.${answer}`] = userId;
          const update = await Poll.findByIdAndUpdate(
            pollId,
            { $push: push },
            { upsert: true }
          );
          res.status(201).json(update);
        } catch (err) {
          error.status = 400;
          next(error);
        }
      } else if (existingVote && existingVote.length > 0) {
        // check if answer is same as previous, if yes send not modified
        if (existingVote.toLowerCase() === answer.toLowerCase()) {
          res.status(304).send("Response already saved");
        } else {
          // delete the previous response and save it in new
          if (
            Array.isArray(poll.options[existingVote]) &&
            poll.options[existingVote].length > 0
          ) {
            // TODO: filtering this is not returning array but 1
            poll.options[existingVote] = poll.options[existingVote].filter(
              (vote) => vote != userId
            );
            poll.options[answer] = poll.options[answer].push(userId);
            const update = await Poll.findByIdAndUpdate(pollId, {
              $set: { options: poll.options },
            });
            res.status(201).json(update);
          }
        }
      } else {
        error = {
          status: 500,
          message: "Something went wrong",
        };
        next(error);
      }
    } else {
      error = {
        status: 404,
        message: "Poll not found",
      };
      next(error);
    }
  } catch (error) {
    error.status = 400;
    next(error);
  }
};

但是每当我通过POSTMAN发布数据时,什么都没得到-在这里您可以看到我将数据发布到mongodb中,而在这里什么也没有得到,我发布了{userId,answer}之类的数据,但是得到了空数组。 在此处输入图片说明

但是当我检查我的ROBO-3T数据库时-我正在获取数据-> 在此处输入图片说明

我的问题是,为什么在将数据获取到数据库而不是POSTMAN结果时会出现这种情况?请帮忙

还我收到这样的警告- (节点:8776)DeprecationWarning:猫鼬:findOneAndUpdate()findOneAndDelete()没有useFindAndModify选项设置为false已被弃用。请参阅:https//mongoosejs.com/docs/deprecations.html#findandmodify

EOL

文档中

默认情况下,findByIdAndUpdate()返回应用更新之前的文档。如果将new设置为true,则在应用更新后,findOneAndUpdate()会为您提供对象。

因此,您可以尝试new在查询选项中设置该选项:

const update = await Poll.findByIdAndUpdate(pollId, {
              $set: { options: poll.options },
            }, {new: true});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用POSTMAN发布数据后未获取数据

来自分类Dev

oracledb - 插入后未获取数据

来自分类Dev

使用available()时未获取全部数据

来自分类Dev

OpenWeather:未获取数据

来自分类Dev

http服务返回后范围变量未获取数据对象

来自分类Dev

单击 AM 图表后未获取图表数据

来自分类Dev

删除laravel中的一行后数据未获取

来自分类Dev

崩溃而未获取json数据

来自分类Dev

RoboSpice - 未获取缓存数据

来自分类Dev

Laravel:未获取所需数据

来自分类Dev

如何获取更改后的 url 以发布数据?

来自分类Dev

使用 FormData 时未获取文件数据

来自分类Dev

WebApi方法未获取发布的数据。什么不见了?

来自分类Dev

联接查询未获取任何数据

来自分类Dev

Jade模板未获取Express传递的数据

来自分类Dev

bash脚本未获取管道数据

来自分类Dev

离子angularjs $ http模块未获取数据

来自分类Dev

PHP,cURL请求未获取数据

来自分类Dev

nextjs安装的组件未获取数据

来自分类Dev

控制器未获取服务数据

来自分类Dev

未获取NotORM外键数据

来自分类Dev

bash脚本未获取管道数据

来自分类Dev

Flask-WTF表单未获取数据

来自分类Dev

卷曲响应未获取结果数据

来自分类Dev

Jade模板未获取Express传递的数据

来自分类Dev

访问json中的数据未获取密钥

来自分类Dev

$ stmt-> fetch()未获取数据

来自分类Dev

Foreach 未获取所有数据

来自分类Dev

Scrapy以下链接未获取数据