Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

javascript - How to grab (orderId) payload value in form body to update the status of the order?

I am working on mern stack e-commerce project . I have a order update route . Order status get updated only by the admin . While updating order status admin gets the default(preload) status . Then admin enter new status for updation .
When I enter (accept) into input fields and hit update button it shows this and status may not get updated . I am not able to grab orderID . enter image description here

enter image description here

Here is my update status backend controller

exports.updateStatus = (req, res) => {  Order.updateOne(
{ _id: req.body.orderId },
{ $set: { status: req.body.status } },
{ new: true, useFindAndModify: false },
(err, order) => {
  if (err) {
    return res.status(400).json({ error: "Cannot update order status" });
  }
  res.json(order);
} );};

update order route

 router.put(
  "/order-update/:userId",
  isSignedIn,
  isAuthenticated,
  isAdmin,
  updateStatus
);

API handler for frontend

export const updateOrder = (userId, token, order) => {
  return fetch(`${API}/order-update/${userId}`, {
    method: "PUT",
    headers: {
      Accept: "application/json",
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(order),
  })
    .then((response) => {
      return response.json();
    })
    .catch((err) => console.log(err));
};
question from:https://stackoverflow.com/questions/66045770/how-to-grab-orderid-payload-value-in-form-body-to-update-the-status-of-the-ord

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are trying to fetch orderId from the body but you are not sending OrderId in the body. Also, I am not able to understand why you sen UserId in route "/order-update/:userId". I think you can send orderId as request param from the front end also you can update your route as

router.put(
  "/order-update/:orderId",
  isSignedIn,
  isAuthenticated,
  isAdmin,
  updateStatus
);

After that, you can get orderId as

  let { orderId } = req.params;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...