Can't array.push

I used array.map so I can add table names that I want to add to my API. but when I loop through it, I can’t push the returned object to the array, but I can console.log() the returned object as an array. Here’s my code.

const contentHandler = async (req, res, db) => {
    // get the argument from get request
    const tableNames = [
        'services',
        'practitioners',
        'network'
    ]

    let tablekey;
    let allTable = [];

    tableNames.map(async(table, key) => {
        tablekey = `table_${key}`
        tablekey = await getData(db, table);

        allTable.push(tablekey);
    })

    res.status(200).json({data: allTable});
}

please remember that map returns a new array, and if you don’t use it it is a waste of resources. Maybe you want to use something else, like forEach?

2 Likes