我在Koa中开发,我使用Firebase进行消息传递,因为它是实时数据库。当我想要从firebase获取消息时,我得不到它,但在console.log()中它会显示给我。
这是我的getConversation函数(消息)
async getConversation(conversationName, callback) {
var ref = await admin.database().ref(`messages/${conversationName}`)
await ref.on('value', (snapshot, prevChildKey) => {
var newPost = snapshot.val()
let values = Object.values(newPost)
callback(values)
})
}然后在另一个控制器中调用它,如下所示
async getMessages(ctx) {
const id = ctx.params.id
const nameOfConversation = await ctx.db.Conversation.findById(id)
await firebaseIndex.fbController.getConversation(nameOfConversation.name, response => {
console.log(response)
ctx.body = response //TODO
})
}最后,我把它称为路由。
router.get('/getConversation/:id', middlewares.isAuthenticate, controllers.userConversation.getMessages)我总是找不到身体。有人知道我怎么解决这个问题吗?
发布于 2018-09-28 19:08:30
我解决了。
async getMessages(ctx) {
const id = ctx.params.id
const nameOfConversation = await ctx.db.Conversation.findById(id)
ctx.body = await new Promise((resolve, reject) => {
firebaseIndex.fbController.getConversation(nameOfConversation.name, async response => {
resolve(response)
})
})
}ctx.body必须有一个承诺。
https://stackoverflow.com/questions/52551395
复制相似问题