英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论