简体   繁体   中英

How to map nested array in javascript(node.js) API?

let getOeReport = async (req, res) => {
    try {
       
        let searchFormatForReason = {
            "query": {

                "reasonCode": null,

            },
            "fields": {
                'reason': 1, '_id': 0
            }
        }
        let result = await oeDao.getOeReport(searchFormat)

        let resultReasons =
            result.map(async res =>
                res.sequences.map(inter =>
                    inter.interrupts.map((interrupts) => {
                        searchFormatForReason.query.reasonCode = interrupts.reason
                        reasonDao.getReasonByFields(searchFormatForReason)

                    })
                )
            )
        console.log(resultReasons)
        res.status(200).json({
            status: "Success",
            data: {
                result: result
            }
        });
    } catch (err) {
        console.log(err)
        res.status(400).json({
            status: "Failure"
        });
    }
};

Here in the above code, If I try to console the resultReasons means I'm getting null. Result variable has nested array of objects so that, I need 'reason' value inside interrupts array which is inside sequences array and that is within the result array. And i should make API request with that value and I could push it into resultReasons array. Please someone help me... Thank You

let resultReasons =
        result.map(async res =>
            res.sequences.map(inter =>
                inter.interrupts.map((interrupts) => {
                    searchFormatForReason.query.reasonCode = interrupts.reason
                    reasonDao.getReasonByFields(searchFormatForReason)

                })
            )
        )

In this piece of your code you are using async so you must be receiving a Promise , since you are not making use of await within that async function I don't see the reason of keeping it, however you can just add an await as per the code below and should fix the issue ( you are already using async on getOeReport so it will work without any modifications )

let resultReasons =
        await result.map(async res =>
            res.sequences.map(inter =>
                inter.interrupts.map((interrupts) => {
                    searchFormatForReason.query.reasonCode = interrupts.reason
                    reasonDao.getReasonByFields(searchFormatForReason)

                })
            )
        )

But you are saying you get null which I don't understand how you are getting it, this because again you are using an async function within the first map, hence you must get a Promise , the only way I see your variable can be null is if the result variable is null, BUT it should trigger an Exception so your console.log will never be excecuted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM