Javascript 在我的第二个查询能够在.map函数内部运行之前,我的承诺已得到解决

Javascript 在我的第二个查询能够在.map函数内部运行之前,我的承诺已得到解决,javascript,asynchronous,Javascript,Asynchronous,你知道如何使它异步吗?我尝试使用async npm包,但在从async.map(数组、函数(结果、回调){//was null here})获取任何类型的响应时遇到问题。正如现在一样,它返回一个空对象,然后在所有帮助者的帮助下记录我的数据!:) 您是否尝试将要映射的数组中的项转换为承诺?然后,使用类似于Promise.all(见下文)的方法?我还继续向前移动了一些项目,以使我更容易阅读 const getAdditionalInfosPromise = (ProgramID, Le

你知道如何使它异步吗?我尝试使用async npm包,但在从
async.map(数组、函数(结果、回调){//was null here})
获取任何类型的响应时遇到问题。正如现在一样,它返回一个空对象,然后在所有帮助者的帮助下记录我的数据!:)

您是否尝试将要映射的数组中的项转换为承诺?然后,使用类似于
Promise.all
(见下文)的方法?我还继续向前移动了一些项目,以使我更容易阅读

       const getAdditionalInfosPromise = (ProgramID, LeadId) => {
        return new Promise((resolve, reject) => {
            connection.query(`SELECT * FROM crm_additionalLeadInfoFields WHERE ProgramID= ${ProgramID};`, (error, response) => {
                if (error) reject(console.error(error, 'SQL ERROR IN GETADDITIONALINFOPROMISE'));
                else {
                    let getAdditionalInfosData = [];
                    const getAdditionalInfos = response;
                    getAdditionalInfos.map((data, i) => {
​
                        let tableNameData;
                        // checking to see what our table name is 
                        if (data.TableName === 'leads') {
                            tableNameData = `WHERE id= ${LeadId};`
                        }
                        else {
                            tableNameData = `Where LeadId = ${LeadId};`
                        }
​
                        // Remove any white spaces
​
                        const columnName = data.ColumnName.replace(/ +/g, "")
​
                        connection.query(`SELECT ${columnName} as pertinentValue 
                        FROM ${data.TableName}
                        ${tableNameData}`, (err, res) => {
                            if (err) console.error(err, 'MY SQL ERR IN GETADDITIONALINFOSPROMISE');
                            else {
                                console.log(data);

                                if (data.DisplayName !== 'Dealer Name') {
                                    const pertinentValue = res[0].pertinentValue
​
                                    getAdditionalInfosData.push({
                                        'id': `additionalItem${i}`,
                                        'label': data.DisplayName,
                                        'value': `${pertinentValue !== null ? pertinentValue : ''}`,
                                        'class': ''
                                    })
​
                                }
                            }
                        })

                    })

                    resolve(getAdditionalInfosData)
                }
            })
​
        })
    }
然后,您可以执行以下操作:

const getAdditionalInfosPromise = (ProgramID, LeadId) => {
    return new Promise((resolve, reject) => {
        connection.query(`SELECT * FROM crm_additionalLeadInfoFields WHERE ProgramID= ${ProgramID};`, (error, response) => {
            if (error) reject(console.error(error, 'SQL ERROR IN GETADDITIONALINFOPROMISE'));

            let getAdditionalInfosData = [];
            const getAdditionalInfos = response;

            const allPromises = getAdditionalInfos.map((data, i) => { 
                if (data.DisplayName !== 'Dealer Name') {
                    const tableNameData = data.TableName === 'leads' ? `WHERE id= ${LeadId};` : `Where LeadId = ${LeadId};`;
                    const columnName = data.ColumnName.replace(/ +/g, "")​

                    return new Promise((resolve, reject) => {
                        connection.query(`SELECT ${columnName} as pertinentValue FROM ${data.TableName} ${tableNameData}`, (err, res) => {
                            if (err) console.error(err, 'MY SQL ERR IN GETADDITIONALINFOSPROMISE');

                            console.log('within the select query', data);


                            const pertinentValue = res[0].pertinentValue​
                            resolve({
                                'id': `additionalItem${i}`,
                                'label': data.DisplayName,
                                'value': `${pertinentValue !== null ? pertinentValue : ''}`,
                                'class': ''
                            })​


                        })
                    })
                }

                reject({});
            });

            console.log(allPromises)

            resolve(getAdditionalInfosData)

        })​
    })
}
Promise.all(allPromises).then(function(values) {
  console.log(values);
  // do anything you need with your data here
});