使用 map 内部的 push 仍然返回 null 数组

huangapple go评论77阅读模式
英文:

Using push inside of map still returns null array

问题

I'm getting a list of processes and I'm trying to map through them and add certain properties to an object, which is then pushed into an array. When I console log it out, it's fine, but in the gql client/front end, I'm getting null results.

let results = []
await list.forEach((process) => {
    results.push({
        name: process.name,
        created_at: process.pm2_env.created_at,
        pm_uptime: process.pm2_env.pm_uptime,
        status: process.pm2_env.status,
        memory: process.monit.memory,
        cpu: process.monit.cpu
    })
})

const contents = await Promise.all(results);
console.log(contents);

I've also tried using map, forEach, and for/of.

I'm not sure what I'm doing wrong? Any advice appreciated.

英文:

I'm getting a list of processes and I'm trying to map through them and add certain properties to an object, which is then pushed into an array. When I console log it out its fine, but in gql client/front end im getting null results.

                    let results = []
                    await list.forEach((process) => {
                        results.push({
                            name: process.name,
                            created_at: process.pm2_env.created_at,
                            pm_uptime: process.pm2_env.pm_uptime,
                            status: process.pm2_env.status,
                            memory: process.monit.memory,
                            cpu: process.monit.cpu
                        })
                    })

                    const contents = await Promise.all(results);
                    console.log(contents);

I've also tried using map, forEach and for/of.

I'm not sure what I'm doing wrong? Any advice appreciated.

答案1

得分: 0

Thanks for the replies. I had some odd behavior with this one but solved it eventually.

I ended up throwing it into a function that returned a promise.

const getPm2List = () => {
  return new Promise((resolve, reject) => {
      pm2.list(async (err, list) => {
          if (err) throw new GraphQLError('Unable to connect to PM2')

          resolve(list)
      })
  })
}

const processList = await getPm2List()

processList.forEach((process) => {
  results.push({
      name: process.name,
      pm_uptime: process.pm2_env.pm_uptime,
      status: process.pm2_env.status,
      memory: process.monit.memory,
      cpu: process.monit.cpu
  })
})

return results
英文:

Thanks for the replies. I had some odd behaviour with this one but solved it eventually.

I ended up throwing it into a function that returned a promise.

const getPm2List = () => {
  return new Promise((resolve, reject) => {
      pm2.list(async (err, list) => {
          if (err) throw new GraphQLError('Unable to connect to PM2')

          resolve(list)
      })
  })
}

const processList = await getPm2List()

processList.forEach((process) => {
  results.push({
      name: process.name,
      pm_uptime: process.pm2_env.pm_uptime,
      status: process.pm2_env.status,
      memory: process.monit.memory,
      cpu: process.monit.cpu
  })
})

return results

huangapple
  • 本文由 发表于 2023年5月10日 22:34:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定