简体   繁体   English

如何修复我的删除按钮? Reactjs、MondogDB 和 Nodejs

[英]How can I fix my delete button? Reactjs, MondogDB and Nodejs

Good day.再会。

In my website that I am building I want the user to be able to delete a promo code from the database but for some reason my delete button isn't working and I am unsure what I am missing.在我正在构建的网站中,我希望用户能够从数据库中删除促销代码,但由于某种原因,我的删除按钮不起作用,我不确定我缺少什么。

When the delete button is pressed it is supposed to remove the promo code completely from the database but instead nothing happens at all.当按下删除按钮时,它应该从数据库中完全删除促销代码,但什么也没有发生。 It just returns to the promo code table and the code still remains there, even after refreshing and fetching the codes again the code is there and double checked the database to see if the code is there as well and the code is.它只是返回到促销代码表,代码仍然保留在那里,即使在再次刷新和获取代码之后,代码仍然存在,并仔细检查数据库以查看代码是否也存在,代码是否存在。

Also I checked the command prompt and the delete function gets received and executed without any errors, the console log doesn't display any errors and in networks it shows that deletepromocodes was created with response code 201我还检查了命令提示符,删除函数被接收并执行,没有任何错误,控制台日志没有显示任何错误,并且在网络中它显示 deletepromocodes 是用响应代码 201 创建的

I am using MongoDB, Reactjs and Nodejs to create the website.我正在使用 MongoDB、Reactjs 和 Nodejs 创建网站。

This is the code in the API:这是API中的代码:

     exports.deletePromoCode = function (req, res) {
        promoCodeModel.PromoCode.deleteOne({
        promoCode: req.body.promoCode
      }, {
      }, function (err, promoCode) {
        if(err) {
          console.log(err);
          return res.status(501).send(err);
        }
        return res.status(201).send(promoCode);
      });
    };

This is the code for the backend in the APP:这是APP中后台的代码:

     export function deletePromoCode() {
      return new Promise(async (resolve,reject) => {
        $.ajax({
          type: "POST",
          url: server + apiCallPromos + 'deletepromocode',
          contentType: 'application/json',
          dataType: 'json',
          crossDomain: true,
          beforeSend: function(xhr) {
            xhr.setRequestHeader('Authorization', 'Bearer ' + sessionStorage.token);
          },
          success: async data => {
            resolve(data);
          },
          error: async(xhr) => {
            if (xhr.responseText) {
              reject( xhr.responseText );
            } else {
              reject("No Connection Found!");
            }
          }
        });
      });
    }

This is the code on the APP file when the delete button is clicked:这是点击删除按钮时APP文件上的代码:

     handleDeletePromoCode = (promoCode) => {
        deletePromoCode(promoCode)
        .then((res) => {
          getPromoCodes();
        })
        .catch((err) => {
          Notify("error", err, 3, "bottomRight");
        });
      };

Finally the Router.js file in the API:最后是 API 中的 Router.js 文件:

const promocodes = require('./promocodes.js');
    app.get('/test/api/promocodes/getpromocodes', jwt({
        secret: secret.secretToken
    }), promocodes.getPromoCodes);
    app.post('/test/api/promocodes/addpromocode', jwt({
        secret: secret.secretToken
    }), promocodes.addPromoCode);
    app.post('/test/api/promocodes/deletepromocode', jwt({
        secret: secret.secretToken
    }), promocodes.deletePromoCode)

Then the code for the delete button:然后是删除按钮的代码:


                    <Column
                  title="Actions"
                  render={(text, promoCode) => (
                    <span>
                      <a
                        onClick={(e) => this.handleEditPromoCode(promoCode)}
                      >
                        Edit
                      </a>
                      <Divider type="vertical" />
                      <a
                        onClick={(e) => {
                          this.handleDeletePromoCode();
                        }}
                      >
                        Delete
                      </a>
                    </span>
                  )}
                />

The Promo Code Model Schema:促销代码模型架构:

 const mongoose = require("mongoose");
 const bcrypt = require("bcryptjs");
 const { Int32 } = require("bson");
 const Schema = mongoose.Schema;
 
 const promoCodeModel = new Schema({
     promoCode: {
         type: String
     },
     discount: {
         type: Number,
     },
     active: {
         type: Boolean,
         default: true,
     },
     delete: {
         type: Boolean,
         default: false,
     }
 }, {
     toJSON: {
         transform: function (doc, ret) {
 
         }
     }
 });
 
 const PromoCodeModel = mongoose.model('promoCodeModel', promoCodeModel, 'promocodes');
 
 exports.PromoCode = PromoCodeModel;
 

all help will be much appreciated thank you, been struggling with this for a while, tried googling the problem, but that didn't help much either所有帮助将不胜感激,谢谢,一直在努力解决这个问题,尝试使用谷歌搜索问题,但这也没有多大帮助

Your front-end sends a POST which asks the back-end to delete the promo code.您的前端发送一个 POST 请求后端删除促销代码。 Which promoCode ?哪个促销代码 The AJAX call has no body, so req.body.promoCode will be undefined. AJAX 调用没有正文,因此 req.body.promoCode 将是未定义的。

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

相关问题 如何在我的 nodejs 应用程序中使用 CORS 修复此错误 - How can I fix this error with CORS in my nodejs application 如何从我的 nodejs 服务器中删除文件 - How can i delete a file from my nodejs server 单击nodejs中的删除按钮后,如何从所有mongodb集合中删除特定ID - How can i delete particular id from all the mongodb collection on click of delete button in nodejs 如何确保只有 object 的所有者才能看到按钮(ReactJS / NodeJS)? - How do I ensure only the owner of the object can see the button (ReactJS / NodeJS)? 我该如何修复 nodejs 中的这个模式错误? - How can i fix this schema error in nodejs? 如何使用 reactjs 前端修复 multer 代码不添加到我在我的服务器中创建的文件夹中的 nodejs - How to fix multer code not adding to my created folder inside my server in nodejs with reactjs frontend 如何在我的 NodeJS 文件操作应用程序中修复此 memory 泄漏? - How can I fix this memory leak in my NodeJS file manipulation application? 如何构建离线 Reactjs 和 Nodejs 环境来开发应用程序? - How can I build an Offline Reactjs and Nodejs environment for developing apps? 如何纠正 ReactJS / NodeJS / MySQL 中的这个错误? - How can I correct this error in ReactJS / NodeJS / MySQL? 我如何从 ReactJS 访问当前 NodeJS 中的环境变量 - How can i access evironment variables in present NodeJS from ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM