简体   繁体   English

FindOneAndUpdate 仅覆盖和更新第一个 id

[英]FindOneAndUpdate overriding and updating only the first id

I am simply trying to update the content according to the id, but whichever id I am using, it updates only the first id by overriding it.我只是想根据 id 更新内容,但无论我使用哪个 id,它都只会通过覆盖第一个 id 来更新它。 The flow goes as routes => controller => repository Following is the code: Routes =>流程如下 routes => controller => repository 以下是代码:Routes =>

router.post("/:pageId/content", async (req, res, next) => {
  try {
    const pageId = req.params;
    const pageContent = req.body;
    if (!pageId || !pageContent) {
      throw {
        statusCode: 200,
        customMessage: "All parameters are required"
      };
    }
    const result: any = await webpageController.createContent(
      pageId,
      pageContent
    );
    if (result.isError) {
      throw result.error;
    }
    res.status(200).json(result.data);
  } catch (error) {
    next(error);
  }
});

Controller => Controller =>

const createContent = async (pageId: any, pageContent: any) => {
  try {
    // calls repository to create content
    const result = await webpageRepository.createContent(pageId, pageContent);
    // if result is not success, throw error
    if (!result.success) {
      throw {
        statusCode: 400,
        customMessage: "Unable to create web page content"
      };
    }

    return {
      isError: false,
      data: result.data
    };

Repository =>存储库=>

export const createContent = async (pageId: any, content: any) => {
  try {
    const result = await webpage.findOneAndUpdate(pageId, { content });
    return {
      data: result,
      success: true
    };
  } catch (error) {
    logger.error(
      `at:"repositories/webpage/createContent" => ${JSON.stringify(error)}`
    );
    return {
      success: false
    };
  }
};

Here it can be seen that the id I have used in the route and the id getting updated is different.这里可以看出我在路由中使用的id和更新的id是不一样的。 What am I doing wrong here?我在这里做错了什么? 在此处输入图像描述

Following is the schema:以下是架构:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const webpage = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      trim: true,
      maxlength: 25,
    },
    slug: {
      type: String,
    //   required: true,
    },
    url: {
      type: String,
      required: true,
      unique: true
    },
    content: Object,
  },
  {
    timestamps: true,
  },
);

export default mongoose.model('webpages', webpage);

I think you should use a dictionary as the parameter.我认为你应该使用字典作为参数。

const result = await webpage.findOneAndUpdate({_id:pageId.pageId}, {content});

You can check on this documentation about how to use the "findOneAndUpdate" https://mongoosejs.com/docs/tutorials/findoneandupdate.html#getting-started您可以查看有关如何使用“findOneAndUpdate”的文档 https://mongoosejs.com/docs/tutorials/findoneandupdate.html#getting-started

This worked for me这对我有用

const result = await webpage.findOneAndUpdate(   {_id:pageId.pageId} , { content });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM