Home > Enterprise >  How can I fix my delete button? Reactjs, MondogDB and Nodejs
How can I fix my delete button? Reactjs, MondogDB and Nodejs

Time:11-17

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

I am using MongoDB, Reactjs and Nodejs to create the website.

This is the code in the 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:

     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:

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

Finally the Router.js file in the API:

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

CodePudding user response:

Your front-end sends a POST which asks the back-end to delete the promo code. Which promoCode? The AJAX call has no body, so req.body.promoCode will be undefined.

  • Related