0

I have a function that reads from a Firebase database and would return any data it finds about a user.

authorize.js:

const login = (db, token) => {
    db.collection('users').get().then(async (e) => {
        var i = []
        e.forEach(async (doc) => {
            if (doc.data().access_token === token){
                i.push(doc.data().id)
                return doc.data()
            }
        })
        if (i.length === 0) return false
    }).catch((error) => {
        return false
    })
}
module.exports = login

app.js:

app.post('/token/:token', async (req, res) => {
    res.send(await login(db, req.params.token))
})

In authorize.js, I catch the error, but don't log it. When I console.log() the value that would've been returned directly inside the function, no errors occur and it gives me my desired result.

2 Answers 2

2

You are missing two returns. Also, you have the function inside the e.forEach async which means it will run after the rest of your code so you won't get your result, remove that (since you aren't doing anything async there anyway):

const login = (db, token) => {
    // v HERE v - you need to return the promise
    return db.collection('users').get().then(async (e) => {
        var i = []
        // v HERE v - no async
        e.forEach((doc) => {
            if (doc.data().access_token === token){
                i.push(doc.data().id)
                // Also, no need for a return here, since it doesn't do anything
            }
        })
        if (i.length === 0) return false
        return i // << instead, here was another return missing
    }).catch((error) => {
        return false
    })
}
module.exports = login

A clearer way would be to make login also async, then you could just use await there as well:

async function login (db, token) {
    try {
        const e = await db.collection('users').get()
        const i = []

        for (const doc of e) {
            if (doc.data().access_token === token){
                i.push(doc.data().id)
            }
        }

        if (i.length === 0) return false
        return i
    } catch (e) {
        return false
    }
}

module.exports = login

This could be further simplified:

async function login (db, token) {
    try {
        const matchingUserIds = await db.collection('users').get()
            .map(doc => doc.data())
            .filter(docData => docData.access_token === token)
            .map(docData => docData.id)

        return matchingUserIds.length ? matchingUserIds : false
    } catch (e) {
        return false
    }
}

module.exports = login

(I wonder though if there isn't any better method to filter the users by access token in your database. Right now it seems like you'll fetch all users and then check for the token in this function; it would probably be more scalable to query the database by token in the first place.)

Sign up to request clarification or add additional context in comments.

Comments

0

An async function always go with an await and am not seeing any await right inside the function.

More clarity.

1 Comment

This should be a comment, not an answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.