Koa.js always get Not Found 404

1.5k Views Asked by At

I develop in Koa and I use Firebase to messaging, because of a real-time database. When I want to get messages from firebase I get Not found, but in console.log() it shows me.

This is my function to getConversation(Messages)

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)
    })
}

Then I call it in another controller like this

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
    })

}

At the last, I call it in routes.

router.get('/getConversation/:id', middlewares.isAuthenticate, controllers.userConversation.getMessages)

I always get body Not found. Do anybody know how I can solve it?

1

There are 1 best solutions below

0
On

I solved it.

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 has to have a Promise.