forEach和带有异步功能的映射

时间:2019-11-07 11:50:07

标签: javascript loops asynchronous

我试图在异步函数中使用forEach和map,但这什么也没返回。

这是我的代码:

async function test(){

    const sqsMessages = [{"eventname":"\\core\\event\\user_list_viewed","component":"core","action":"viewed","target":"user_list","objecttable":"course","objectid":"2","crud":"r","edulevel":0,"contextid":25,"contextlevel":50,"contextinstanceid":"2","userid":"2","courseid":"2","relateduserid":null,"anonymous":0,"other":"{\"courseshortname\":\"324234\",\"coursefullname\":\"Teste\"}","timecreated":1572957245,"origin":"web","ip":"127.0.0.1","realuserid":null}];
    const queryValues = sqsMessages.forEach( logs => {
        return `(null,${ [
          ...Object.values(logs).map(async val => {
            if (typeof val === 'number') {
              return await val;
            }
            if (typeof val === 'string') {
              return await `'${val}'`;
            }
            return await `${val}`;
          }),
        ].join(',')})`;
      });


      return queryValues;
}

当我尝试保证出现此错误时:

async function test(){

    const sqsMessages = [{"eventname":"\\core\\event\\user_list_viewed","component":"core","action":"viewed","target":"user_list","objecttable":"course","objectid":"2","crud":"r","edulevel":0,"contextid":25,"contextlevel":50,"contextinstanceid":"2","userid":"2","courseid":"2","relateduserid":null,"anonymous":0,"other":"{\"courseshortname\":\"324234\",\"coursefullname\":\"Teste\"}","timecreated":1572957245,"origin":"web","ip":"127.0.0.1","realuserid":null}];
    const queryValues = sqsMessages.forEach( logs => {
        return `(null,${ [
          ...Object.values(logs).map(async val => {
            if (typeof val === 'number') {
              return await val;
            }
            if (typeof val === 'string') {
              return await `'${val}'`;
            }
            return await `${val}`;
          }),
        ].join(',')})`;
      });

      const resultado = await Promise.all(queryValues);

      return resultado;
}
TypeError: undefined is not iterable

我在做什么错了?

0 个答案:

没有答案