-3

i am trying to add fileUrl property to the order object inside async function but it won't work

i expect the order object to have a fileUrl property after adding it but it won't work

    router.get('/my-orders/:id', isAuth, async (req, res) => {
    const id = req.params.id;

    try {
        const orders = await Order.find({ userId: id });
        if (orders.length > 0) {
            for (const order of orders) {
                const getObjectParams = {
                    Bucket: bucketName,
                    Key: order.fileName,
                }

        
                const command = new GetObjectCommand(getObjectParams);
                const url = await getSignedUrl(s3, command, { expiresIn: 3600 });
                // adding fileUrl property to order
                order.fileUrl = url;
                
                // it logs only order without fileUrl property
                console.log(order);
            }

            res.send('OK');
        }
    } catch (error) {
        console.error(error);
    }
})
  • 2
    What do you mean "it won't work"? What debugging steps did you do? What did you see in those debugging steps? Since your code doesn't actually do anything with any of these order objects, where is the rest of the code here that actually does something with the orders. I would guess that the real problem is in the rest of the code that you aren't showing us. – jfriend00 Nov 05 '22 at 18:01
  • i have an array of orders each order contains some properties i am looping over those orders and i want to add a fileUrl property to each order after getting the fileUrl from amazon bucket but when i "append" the fileUrl to order and after logging it it does not get added to the order object – LAHOUCINE IJIOUI Nov 05 '22 at 18:32
  • Does your database require a `.toObject()` call to convert a database result to a full-fledged Javascript object? – jfriend00 Nov 06 '22 at 00:13
  • Are you 100% sure that the `url` you are getting and assigning to the property actually has a value? If you `console.log(url)` do you see your value? – jfriend00 Nov 06 '22 at 00:27
  • yes when i console.log(url) it logs the url but when trying to add nothing happens – LAHOUCINE IJIOUI Nov 06 '22 at 11:00

1 Answers1

0

In MongoDB, iterating a cursor (which is what Order.find() gives you) will give you MongoDB documents, not plain Javascript objects. If you want plain Javascript objects that you can then manipulate like a plain Javascript object, then use .toObject() on the document.

router.get('/my-orders/:id', isAuth, async (req, res) => {
    const id = req.params.id;

    try {
        const orders = await Order.find({ userId: id });
        if (orders.length > 0) {
            for (const order of orders) {
                const getObjectParams = {
                    Bucket: bucketName,
                    Key: order.fileName,
                }


                const command = new GetObjectCommand(getObjectParams);
                const url = await getSignedUrl(s3, command, { expiresIn: 3600 });

                // convert from monogdb document to plain Javascript object
                const modifiedOrder = order.toObject();

                // adding fileUrl property to order
                modifiedOrder.fileUrl = url;
                console.log(modifiedOrder);
            }

            res.send('OK');
        } else {
            res.send('No Orders Found');
        }
    } catch (error) {
        console.error(error);
        // always send some response, even for errors
        res.sendStatus(500);
    }
});

Note, I also cleaned things up so that this request handler is always sending a response, even when no orders are found or when there's an error.


For further discussion of other options, including .lean() and .set(), see this answer.

jfriend00
  • 683,504
  • 96
  • 985
  • 979