node.js - Koa.js 总是找不到 404
问题描述
我在 Koa 中开发并使用 Firebase 进行消息传递,因为有一个实时数据库。当我想从 firebase 获取消息时,我得到了 Not found,但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)
我总是找不到身体。有人知道我该如何解决吗?
解决方案
我解决了。
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
必须有一个Promise。
推荐阅读
- c++ - Cmake 问题:找不到“FreeImage.h”文件
- python - 当我尝试使用 python 和 django 重定向到购物车时,我不断收到此错误
- discord.js - 无法读取未定义的属性“删除”
- docker - 操作系统与 docker 镜像有什么关系?
- angular - Angular 9:找不到命名空间“FullCalendarVDom”?
- javascript - 我在 ajax 中的 XMLHttpRequest 在 Chrome 中不起作用
- node.js - 如何在 Azure Web App 上安装纤程包
- python - 如何修复 PytestAssertRewriteWarning?
- python - 使用特征值对 3D 特征向量数组进行排序
- webmail - Roundcube 设置文件的位置