English 中文(简体)
I am trying to update my stock in the mongoDb database. After Every purchase one stock item will be subtracted .Update multiple id s in the API
原标题:

I am trying to update my stock in the MongoDB database. After Every purchase one stock item will be subtracted. Update multiple ids in the API that subtract the available amount from the database.

This is the handler code:


    const { _id, availableQty } = req.body;

    console.log(req.body);
    for (let i = 0; i < req.body.length; i++) {
      const doc = await Product.updateOne(
        { _id },
        {
          availableQty,
        }
      );
    console.log(doc);
    }
    return res.json(true);

This is the Axios call. This Info below is also a response from an API

let info = res.data;
const items = info.line_items;
items.forEach(productInfo);
function productInfo() {
  let p_id = items.map((p) => ({
    _id: p.product_id,
    availableQty: p.product_qty - 1,
  }));
  axios
    .put( /api/products/updateproducts , p_id)
    .then((res) => {
      console.log(res.data);
    })
    .catch((res) => {
      console.log(res);
    });
}

This is the console.log of res.body in API handler:

[
  { _id:  646e4ff2707bca32ac9c9a00 , availableQty: 6 },
  { _id:  646e4fe4707bca32ac9c99ff , availableQty: 6 },
  { _id:  646e4fd4707bca32ac9c99fe , availableQty: 6 },
  { _id:  646e4fc9707bca32ac9c99fd , availableQty: 6 }
]

[
  { _id:  646e4ff2707bca32ac9c9a00 , availableQty: 6 },
  { _id:  646e4fe4707bca32ac9c99ff , availableQty: 6 },
  { _id:  646e4fd4707bca32ac9c99fe , availableQty: 6 },
  { _id:  646e4fc9707bca32ac9c99fd , availableQty: 6 }
]

[
  { _id:  646e4ff2707bca32ac9c9a00 , availableQty: 6 },
  { _id:  646e4fe4707bca32ac9c99ff , availableQty: 6 },
  { _id:  646e4fd4707bca32ac9c99fe , availableQty: 6 },
  { _id:  646e4fc9707bca32ac9c99fd , availableQty: 6 }
]

[
  { _id:  646e4ff2707bca32ac9c9a00 , availableQty: 6 },
  { _id:  646e4fe4707bca32ac9c99ff , availableQty: 6 },
  { _id:  646e4fd4707bca32ac9c99fe , availableQty: 6 },
  { _id:  646e4fc9707bca32ac9c99fd , availableQty: 6 }
]
问题回答

暂无回答




相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...

热门标签