Warm tip: This article is reproduced from serverfault.com, please click

JavaScript console.log showing Object as Array?

发布于 2020-11-30 19:48:22

I am using Firebase Auth to create users and logging the error so that I can display it to the user. This logging is currently in test and goes as follows:

console.log(error);
console.log(typeof(error))

In my console I get the following:

[Error: [auth/invalid-email] The email address is badly formatted.]
object

Usually when I get an object it is returned in some kind of {} format so I am confused why it is now in seemingly-array-like format? Could someone shed on light on how this works?

Questioner
beyondtdr
Viewed
0
Matt 2020-12-01 04:01:52

In Javascript, arrays are actually objects with some additional functionality. So typeof([]) will return object.

It looks like the api is returning an array of Error objects. If you wanted to display those errors to the user, you could loop through the returned array and print/concatenate each Error's message property.