Home > Blockchain >  Not receiving response for axios.put(...) eventhough document is updating in mongoDB
Not receiving response for axios.put(...) eventhough document is updating in mongoDB

Time:09-18

I am using axios in React for updating database in mongodb.

 const handleEditFormSubmit = async (editdata) =>{
    try{
    const id = tabledata[editCategory-1]._id;
    let body ={}
    body = {...body, ["Category"]:editdata[`Category${editCategory-1}`]}
    if (editdata[`Asset${editCategory-1}`] !== undefined){ 
    body = {...body, ["Asset"]:editdata[`Asset${editCategory-1}`],["subCategIndex"]:subCateg.subCategIndex}
    }
    const response = await axios.put(`http://localhost:5000/api/assetmaster/update/${id}`, body)
    console.log(response);

    setSentData(body);
    }catch(err){}
  }

My rest API is

    router.put("/update/:id", (req, res)=> {
    const dbConnect = dbo.getDb();
    const listingQuery = { _id: ObjectId(req.params.id) };
    const index = req.body.subCategIndex;
    //Error Handling if null value is recieved
    let objForUpdate = {};    
    if(req.body.Category) objForUpdate = {...objForUpdate, ["Category"]:req.body.Category};
    if(req.body.Asset) objForUpdate = {...objForUpdate, ["Asset."  index   ".Name"]:req.body.Asset};        
    const updates = {
      $set: objForUpdate
    };

    dbConnect
      .collection("assetmaster")
      .updateOne(listingQuery,updates,(err, result)=> {
        if (err) {
          res.status(400).send(`Error updating likes on listing with id ${listingQuery.id}!`);
        } else {
          console.log("1 document updated");
        }
      });
  });

The database connection file is

const {MongoClient} = require('mongodb')
const dotenv =require("dotenv");

dotenv.config();

const client = new MongoClient(process.env.ATLAS_URL);

let database;

module.exports = {
    connectToServer: (callback) => {
        client.connect( (err,db) => {
            if(err || !db ){
                return callback(err);
            }

            database = db.db("inventory");
            console.log("Successfully Connected to MongoDB")

            return callback();
        });
    },
    getDb:  () => {
        return database;
    }
}

I am able to update the documents successfully in the database, however, i am not getting a response using axios.put. Further in the function, the code below the axios.put is also not executing. It seems the axios.put process is done but not completed or closed. Do I need to close the database connection or something else to ensure the process is finished?

CodePudding user response:

You send a response only in the error case, but not in the success case. Add a res.send or similar:

if (err) {
  res.status(400).send(`Error updating likes on listing with id ${listingQuery.id}!`);
} else {
  console.log("1 document updated");
  res.send("1 document updated");
}

Without that, the client awaits a response that never comes, therefore the code before the await axios.put(...) is never executed.

  • Related