JavaScript console.log showing Object as Array? - javascript

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?

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.

Related

The "PushSubscription" object differs in the documentation

I'm trying to implement a push notification system on my site, and the documentation says that I need to store the PushSubscription object returned by the PushManager.subscribe method in the database.
The problem is that in the documentation the PushSubscription object does not have an auth property (as shown here), but when I console.log the object returned by the PushManager.subscribe method, it does have that field.
Something like this:
{
"endpoint":"https://fcm.googleapis.com/fcm/send/cg6PtxFx...",
"expirationTime":null,
"keys":{
"p256dh":"BKvv8i80Eqai6TnPhviquFhrKDdvHXPWkPWNjLtVuE7oxt...",
"auth":"BX2FxMZMAk0VUivVlxWRzw"
}
}
Where can I find more info about the keys field of the PushSubscription object?

Cannot read properties of undefined (reading '0') - ( empty error JSON response with postman)

so i'm working with Joi for validation, and i've encountered this error when trying to post with postman.
i'm follwing a tutorial, i tried to write it differently, but still have the same issue.
i'm trying to access the error message. ( first selecting the error, then the details, then the message )
in the tutorial, it looks like this
res.send(error.details[0].message)
i can see the error, but once i select details, the response is empty.
let me know if you need anything else.
Thank you in advance.
The Lord be with you and save you all, your families and friends :)
It seems like error does not have a property called details. That's why error.details is undefined. Thus, when trying to access the element of the first index of the value undefined you'll get an error.
To fix:
Make sure the error object contains a property details of type Array
If error.details will depend on other code blocks (sometimes it is defined, sometimes it isn't), you can add a ternary expression to tell your code what to in case error.details is indeed undefined.
Example:
err.details ? res.send(error.details[0].message) : res.send("error")
Which translates to
if (err.details) { // if defined
res.send(error.details[0].message) // Send the message from the error
} else {
res.send("error") // Send a general message
}

Call a specific array in a JSON API call

I'm trying to call the API but it doesn't return me anything.
I'm calling it like: res.on('data', d => { const RetrieveFromApi = JSON.parse(d).FLUX.quote.USD.price;.
The API is successfully called since I can see it in the API history, and data is defined as d.
Can you help me please?
from your screen provided in comment, you passed one property you don't access to it which is data
So just add property data before FLUX
JSON.parse(d).FLUX.quote.USD.price;
^data.FLUX.quote.USD.price;
From the image of the log the object seems incomplete at the end, did you try to use JSON.stringify(d) to see if there are any errors on the json object? or copy the response on https://jsonlint.com to check?
Because the error message points that the parse doesn't find a valid json object, maybe that is the whole point.
And the other tip is to try to store the value of JSON.parse in an variable before trying to access his values.

How compare an array of objects using chai?

I am trying to create a test to verify that when I send a request to add a provider, that value is stored in the database.
The test is sending the request, the provider is created, but when I am trying to add the assertions to check the values in the database, I can't.
let providerBd = await functions.getProvider(addProviderRequest.senderID);
console.log(providerBd);
expect(providerBd.razonsocial).to.equal(testCase.provider.senderAdminName);
Code
First I am logging the object with the value obtained in the DB. But the test fails because the value that it gets is undefined and I am expected 'Proveedor Mocha' in razonsocial.
Output
Thanks in advance!
My error is in this line:
expect(providerBd.razonsocial).to.equal(testCase.provider.senderAdminName);
It should be:
expect(providerBd[0].razonsocial).to.equal(testCase.provider.senderAdminName);
Now works :)

Trying to get a snapshot of a variable from firebase gives me an error

Problem
In a social media app I am making with react native and firebase, I am trying to grab the number of comments a post has using the snapshot function of a variable I have saved on my servers, then I am going to add one to this variable when a user adds a new comment. My code to do so is right here:
firebase.database().ref('posts').child(this.state.passKey).update({
comments: firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
})
When I actually run this code, I get an error saying:
Reference.child failed: First argument was an invalid path = "undefined".
Paths must be non-empty strings and can't contain ".","#","$","[", or "["
At first I thought this might be that the "this.state.passKey" wasn't actually passing the key, but putting in a key I copied from the server didn't fix the problem.
My Server
-
To get the comments of particular post you should do like this
let postId='someId'
postRef=`/posts/${postId}`
firebase.database().ref(postRef).once("value", dataSnapshot => {
comment=dataSnapshot.val().comments
});
It looks like you're expecting this bit of code to query the database:
firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
Unfortunately, it doesn't work that way. There's no snapshot property on a database Reference object returned by child() or ref().
Instead, you'll need to query the database at that reference, then when you're called back with its value, you can apply it elsewhere.
var ref = firebase.database().ref('posts/'+this.state.passKey+'/comments')
ref.once('value', function(snapshot) {
// use the snapshot here
})

Categories

Resources