【发布时间】:2018-08-26 07:26:52
【问题描述】:
我正在向服务器发出请求以获取一些值并将它们推送到数组中
我的 GraphQL 服务器在进程结束之前返回给我数组,因此返回一个空数组。这里复制我使用的两种方法:
1-
var array= [];
var auths= authModel.find({}).exec()
.then(res => {
res.map(el =>{
array.push(el.ip)
})
}).then(() => array)
console.log(auths) // object Promise
console.log(array) // []
2-
const auths= authModel.find().exec();
if(!auths){
throw new Error("Error while fetching users...")
}
console.log("auths:", auths) // Promise { <pending> }
return auths // on graphiql > "auths": { "ip": null }
出了什么问题?承诺解决后如何让我的元素返回?
任何提示都会很棒, 谢谢
【问题讨论】:
标签: javascript node.js function promise graphql