Can only access some javascript object keys - javascript

I am noticing strange behavior from a javascript object in my Node application. The user object is created by parsing a csv file with two columns: Email and ID. For some reason, I can only access one of the fields using the typical methods. The other field always returns undefined. I am using Node 15.12.0.
When I log the user object, it returns: { 'Email': 'email#example.com', ID: '12345' }
Curiously, the ID field does not have quotes when it is logged, but the Email field does have quotes.
I can access user.ID as usual, but user.Email returns undefined.
console.log(Object.keys(user)); // [ 'Email', 'ID' ]
console.log(user.ID); // 12345
console.log(user.Email); // undefined
console.log(user['Email']) // undefined
console.log(user["Email"]) // undefined
console.log(user.email) // undefined
console.log(user["\'Email\'"]) // undefined
console.log(user["'Email'"]) // undefined
console.log(user[Object.keys(user)[0]]) // email#example.com
console.log(Object.values(user)[0]) // email#example.com
I have tried the using JSON.parse(JSON.stringify(user)), but I get the same results as before.
The only way I can access the Email field is by using the user[Object.keys(user)[0]] or Object.values(user)[0] which is very strange to me.
I would appreciate any ideas you have to figure out whats happening here. Is there something I can do to examine the object more closely? Does the quotes around the keys indicate anything?

Related

FirebaseError: Function setDoc() called with invalid data. Unsupported field value: undefined

OrderDetails Logged image
I want to save order details to my firestore db,Here my orderDetails:
const orderDetails=[{
id:"ID1",
name:"foo"
},
{
id:"ID2",
name:"foo-foo"
},
]
Here is the code for adding this order Details to fire store:
.then(({paymentIntent})=>{
if(user){
console.log(basket);
const userRef=doc(db,'shopDB',user.uid);
const userOrderRef=doc(userRef,'userOrderInfo',paymentIntent.id);
setDoc(userOrderRef,{
orders: basket,
created :paymentIntent.created,
amount :paymentIntent.amount
},{merge:true})
Here all working if i set document without orderDetails,Other two values will added to fire store,but when i try to add orders to database it throws error
error>>>>>>>>>>>
FirebaseError: Function setDoc()
called with invalid data. Unsupported
field value: undefined
Can anyone help me with this?
looks like 1 of the 2 missing:
orderDetails is undefined
userOrderRef is undefined
try printing them both to make sure what's missing there
Edit:
now that I can see your console.log, your issue is with field alternative:undefined at element in index 1 according to your photo.
remove this property before trying to save it on firestore

How to get value out of Observable Array nested object

I have searched here for an answer to this question with no luck.
I have an observable array which contains only one entry:
I have it stored in self.user()
POSData.Users.getByEmail(sEmail)
.then(data => {
//console.log(data)
self.user.push(data);
})
Now I simply want to extract a few values and assign them to their own observables, BUT... I can't.
I have tried the following to get the firstName...
console.dir(self.user());
//console.log(self.user()[0].data.firstName());
//console.log(self.user().firstName());
//console.log(self.user().data.firstName());
//console.log(self.user()[0].data.firstName());
//console.log(self.user().data[1].firstName());
Does anyone know how to drill down and get to the information I want?
Thanks for looking.
John
You're storing the raw data you got back from your service into your array. You should access members of that data in that form. The firstName property is not an observable, it's just a string in the data property so you shouldn't be calling it as if it was an observable. The only observable in your example is apparently self.user.
Based on your screenshot your new data looks something like this:
{
data: {
firstName: 'John',
lastName: 'Smith'
},
message: 'User retrieved successfully',
status: null
}
If you want to get the first name of this object in your user array, you'd access it like this:
self.user()[0].data.firstName

Beacon prefill function

I already set up the Beacon 'identify' stuffs, and can open up using Beacon("open");
But when I try to use prefill function, i'm getting some error.
Beacon("prefill", {
name: "Steve Aoki",
email: "steve#aoki.com",
subject: "Need help with invoice",
text: "Hello, I need some help with my invoice. See attached.."
})
Error: Uncaught TypeError: Cannot read property 'filter' of undefined
Any code that I missed? Thanks in advance.
Reference here
You need to have a fields array, because in the example you posted, the one thing your code doesn't have is a fields array. This is also shown because filter is an array method, and calling fields.filter when you don't have fields will result in an undefined error.

In node.js how to extract uid from returned facebook providerData json array?

I've got my users signing in with facebook and that gets stored in firebase auth. In index.js i've got an onCreate function that stores some facebook related data in firestore.
When i log to cloud functions console event.data.providerData I get this:
[ { displayName: 'xxxx xxxxx',
photoURL: 'https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/xxxxxxxxx_439xxx336xxxxxx.jpg?oh=bcxxxxxxxxxxx431ce&oe=xxxx4xxx3',
providerId: 'facebook.com',
uid: 'xxxxx725xxxxxx80' } ]
In my index.js file i've set this as
const providerData = event.data.providerData;
This always confuses me and i've read about it a lot.
These are my questions:
Is this a javascript object? Or a JSON object? Or a JSON array?
Does this need to be parsed? eg. JSON.parse(event.data.providerData)? What is JSON.parse actually doing?
To get access to the uid I have tried:
providerData[3]
providerData.uid
providerData["uid"]
providerData['uid']
JSON.parse(providerData) - and then all the above again
var obj = JSON.parse(providerData);
console.log( obj.uid );
I know there are plenty of posts out there re: similar topics and I think i've read a lot of them.
Thanks.
It's an array containing a JSON object at index 0.
The javascript interpreter is automatically parsing Valid JSON as a Javascript object.
Knowing that, you can now access directly the properties of your object like this:
providerData[0].displayName
providerData[0].photoURL
providerData[0].providerId
providerData[0].uid // <-- Your use case

Reading from JSON returns undefined

I'm trying to read from a JSON that contains my error messages, so that in case I'd ever want to change what my error messages say, I'd just change the JSON instead of diving into my source code. Everything seems to be working fine...
var fs = require('fs')
console.log("Now reading from error messages configuration file...");
var errorMsgs = JSON.parse(fs.readFileSync('config/error_msgs.JSON'));
console.log(errorMsgs)
This is what's in errorMsgs.json:
{
"bad_password" : [
{
"error" : "Incorrect login credentials.",
"details" : "This happens if you either have an incorrect username or password. Please try again with different credentials."
}
],
"missing_fields" : [
{
"error" : "Credentials failed to parse.",
"details" : "This happens when one or more fields are missing (or have illegal characters in them), please try again with different credentials."
}
]
}
When I log to the console, errorMsgs displays fine. When I log one of the items that errorMsgs has (like bad_password), it also works fine, as in it displays the items that are nested inside. However, when I attempt to retrieve a specific value like errorMsgs.bad_password['error'], it returns undefined. I can't seem to figure it out. I tried dot notation (errorMsgs.bad_password.error), which returns undefined. I tried the method above (errorMsgs.bad_password['error']) which also returns undefined. Asking for the typeof of errorMsgs, it returns object, which I assume makes it not a string. Passing the value to a variable first and then logging the variable doesn't do anything either. Is node converting it to a string on-the-fly, causing it to return undefined, or am I just doing something wrong?
bad_password" : [
{
"error" : "Incorrect login credentials.",
"details" : "This happens if you either have an incorrect username or password. Please try again with different credentials."
}
],
Your nested object is contained in an array.
errorMsgs.bad_password[0]['error']
This is what you're looking for. Just grab the first value of the array

Categories

Resources