RedKnight91
RedKnight91

Reputation: 400

Why is this Firebase Function Promise not returning the correct Error?

This is a test Firebase Function I'm writing: I already know that ref.child(uuid) does NOT exist in my Realtime DB.

ref.child(uuid).once('value')
    .then((user) => {
        if (!user.exists()) {
            console.log("Throwing, User not found")
            throw new Error("Error: No record with this UUID in Database");
        }

        //Get the node for the given provider (twitter, google or facebook)
        if (user.child(provider).exists())
            return (user.child(provider).val().status)
        else
            throw new Error("Error: Login node does not exist for this provider");
    })
    .then((result) => res.status(200).json({status: result}))
    .catch((error) => res.status(400).json({error_msg: error}));

Output

Throwing, User not found

400, {"error_msg": {}}

So the throw new Error() line is run, but the catch is somehow not getting the error message from the 'error' parameter.

How is this possible?

Upvotes: 1

Views: 93

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

The Error constructor contains two properties message and name, therefore change:

{error_msg: error}

into this:

{error_msg: error.message}

For reference check the following:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error

Upvotes: 1

Related Questions