mongodb js.js路由使用_id字段返回数据库查询的路由值

p4rjhz4m  于 2023-11-17  发布在  Go
关注(0)|答案(1)|浏览(147)

我有以下的js.js路由

router.post('/', async (req, res) => {
       try {
        console.log(`Request Body :\n${req.body}`);
        const {orderIDs} = req.body;
        console.log(`Order IDs : \n${orderIDs}`);

        for (const orderID of orderIDs) {
            const order = await Purchases.findById(orderID);
            console.log(order)
            const orderData = order.orderData;

            // Update stock Levels
            for (const item of orderData) {
                const product = await Products.findOne({productCode: item.productCode});
                await increaseQuantity(product.quantity, item.quantity, item.productCode);
            }
            // Update Purchases Table
            await updateOrderStatusPurchases(orderID);
            // Update TxnAudit Table
            await updateStatusTXN_AUDIT(orderID);
        }

        res.status(200).json({ message: 'Pending orders received'});
    } catch (error) {
        logger.error(error);
        res.status(500).json({ error: 'An error occurred while canceling orders' });
    }
  });

字符串
出于某种原因,每当我尝试使用_id字段从数据库中检索特定订单时,都会导致空值,即使该文档确实存在于数据库中。
我试过用await Purchases.findOne({"_id": orderID)替换await Purchases.findById(orderID),但没有成功。
我还确保Atlas用户具有正确的权限,并且我正在连接到正确的mongodb示例。
任何帮助将不胜感激。
Thanks in advance

更新

请求来自Atlas控制台的数据库触发器,如下面的代码所示

import axios from 'axios'

exports = async function updatePurchases() {
    const purchasesCollection = context.services.get("LegendaryShields").db("test").collection("purchases");

    const twoDaysAgo = new Date();
    twoDaysAgo.setDate(twoDaysAgo.getDate() - 2);

    const query = {
        status: { $in: ["Awaiting payment", "TXN_AUTH_UNSUCCESSFUL", "TXN_FAILED"] },
        purchasedAt: { $lte: twoDaysAgo }
    };

    const projection = { _id: 1 };

    try {
        const purchases = await purchasesCollection.find(query, projection).toArray();
        const orderIDs = purchases.map((purchase) => purchase._id);

        const url = "http://ec2-13-246-182-9.af-south-1.compute.amazonaws.com:5300/cancel_order";

        const response = await axios.post(url, { orderIDs }, {
            headers: {
                'Content-Type': 'application/json'
            }
        });

        if (response.status === 200) {
            console.log(`API response: ${JSON.stringify(response.data)}`);
        } else {
            console.error(`API request failed with status: ${response.status}`);
        }
    } catch (error) {
        console.error(error);
    }
};


至于请求体,如下所示:
请求正文:[object Object] Order ID:651a92d88799f214af2e8a5d,651c5aab28a1a56b70f8f4c8,652803ff12b17ae3aef24e15,6528e7eb427803d97a70abcf,6528ea3d427803d97a70abdf

b4lqfgs4

b4lqfgs41#

router.post('/', async (req, res) => {
       try {
        console.log(`Request Body :\n${req.body}`);
        const {orderIDs} = req.body;
        console.log(`Order IDs : \n${orderIDs}`);

        for (const orderID of orderIDs) {
          // =====> console.log(orderID) can you log the order ID 
          // ====> you might loop  orderIDS.split(',')
            const order = await Purchases.findById(orderID);
            console.log(order)
            const orderData = order.orderData;

            // Update stock Levels
            for (const item of orderData) {
                const product = await Products.findOne({productCode: item.productCode});
                await increaseQuantity(product.quantity, item.quantity, item.productCode);
            }
            // Update Purchases Table
            await updateOrderStatusPurchases(orderID);
            // Update TxnAudit Table
            await updateStatusTXN_AUDIT(orderID);
        }

        res.status(200).json({ message: 'Pending orders received'});
    } catch (error) {
        logger.error(error);
        res.status(500).json({ error: 'An error occurred while canceling orders' });
    }
  });

字符串

相关问题