如何返回以下 _.map + Promise 函数
How to return the following _.map + Promise function?
我有两个函数,一个用于查找 Parse 对象,另一个使用该查找函数并循环遍历对象:
店铺.js:
const store = {}
const Document = Parse.Object.extend('Document')
store.find = () => {
const query = new Parse.Query(Document)
return query.find()
}
store.fetch = () => {
return store.find().then((results) => {
_.map(results, (result) => {
return result.toJSON()
})
})
}
现在我只是做console.log()
来记录输出:
主.js
console.log(store.fetch())
但是console.log
记录了这一点:
ParsePromise {_resolved: false, _rejected: false, _resolvedCallbacks: 数组[0], _rejectedCallbacks: 数组[0]}
如何修改代码以获取实际的对象数组?
-
您的
fetch
函数必须返回这样的_.map
结果store.fetch = () => { return store.find().then((results) => { return _.map(results, (result) => { return result.toJSON() }) }) }
以便附加到
store.fetch
的处理程序将获取由_.map
返回的数组对象 -
store.fetch()
将返回一个 Promise 对象,该对象将异步解析。所以你需要附加一个then
处理程序并打印结果,就像这样store.fetch().then((result) => console.log(result));
相关文章:
- 使用promise和mongoose对文档进行排序
- 测试Angular Service解决错误回调中的promise
- 在下划线中使用_(obj).map(callback)和_.map(obj,callback)之间的区别
- 节点协同与生成器和Promise并行流量控制
- 使用map来检查是否为真'不起作用
- 根据是否解析了 Promise 从函数返回值
- 回调函数中传递参数的困难(Google Map API Markers)
- 将一个方法转换为promise:Nodejs
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- 使用API在我的网站Google-Map上显示搜索地址的纬度和经度
- js promise没有正确关闭
- Angularjs使用“;这个“;promise内的关键字回调
- 如何使用bluebird将原始数据与Promise.map的响应一起传递
- Promise.map未完成,因为后续的Promise.ejoin先完成?承诺
- 使用 Promise.map 返回一个与 bluebird “订购”的对象
- 如何返回以下 _.map + Promise 函数
- 使用q promise和map来获取文件内容
- 如何在任意数量的链元素上使用map或reduce来运行promise-then链
- 类型错误:'未定义'不是对象(正在评估'promise.data.map')
- Bluebird Promise.map不起作用