Access $key from object returned from firebase - javascript

I have an object downloaded from firebase which is of type Bug (Custom type). However, when I tried to console log the output with the following code
console.log('bug',this.bug);
I get the following result. It is what I expected where I can go on and do things like
console.log('company', this.bug.companyName)
However, I want to get that key value as well. Howe can I do that? I tried to do
console.log('company', this.bug.key)
console.log('company', this.bug.$key)
and both of them does not work

try to run
console.log('company', this.bug["$key"])
remember property lookup on objects is either
obj.someKey // or
obj["someKey"]

Related

Why is my console.log in LWC showing variable data in proxy handler

I'm trying to console.log a variable's value but on the browser console instead of printing the variable (an object in my case), I am getting a Proxy container with format like
Proxy {}[[Handler]]: En[[Target]]: Array(0)[[IsRevoked]]: false
On opening the [[Handler]], I get some inner properties which contains an originalTarget property.
On expanding the originalTarget , my data is shown.
How do I get this data to show properly in console and also access it in my LWC ?
this.variableName returns value in a Proxy
If you want to view proxy data so use this :
JSON.stringify(this.caseList)
And further if you want to use it in your lwc use this:
let cases = JSON.parse(JSON.stringify(this.caseList))
console.log(cases);
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Reason : whenever we mark any Javascript object as #track, Salesforce wraps the object inside creating proxy objects.
Solution: #Rajat Jaiswal answer.

Trying to turn an array of entries into an object but doesn't work, why?

I'm trying to turn an array in which every element is an array containing the key, and the value(like entries), I'm trying to convert it back to an object element by the help of the method Object.fromEntries(array) but there seem to be a problem.
This is the error I get: TypeError: Object.fromEntries requires the first iterable parameter yields objects
Here's how I use the method:
const saved = JSON.parse(Object.fromEntries(localStorage.getItem(key)))
The local storage value it should retrieve:
[["$$typeof",null],["type",null],["key",null],["ref",null],["props",{"data":"a data containing collections of objects(fetched from an API)"}]
Since it might be helpful, what I'm trying to do is to store a react component(yes, I work on ReactJS) to local storage and get it back when necessary.
If you don't think that this portion of code is not the reason why this issue appears, please do let me know.

How to check wether or not a value is contained in an array when the value type is ObjectId?

I fetch an array that is stored in mongoose with ObjectIds. After I output this array through the console I get the following output:
["5a5f7199105cc908e874d74b","5a5f9199105cc908e874d74b"]
At the same time fetch a different object from mongoose. If I output the ObjectId of this object through the console (object.id) I get the following output:
5a5f7199105cc908e874d74b
I try to check whether the value is contained in the array or not. My code looks like this:
myArray.includes(myObject.id)
and I expect to have the result of true. Unfortunately, I get false as a result even though The value is in the array. Maybe it has to deal with the OBject Type? I really do not know how to solve this. Thanks.
Because in case of Array ["5a5f7199105cc908e874d74b","5a5f9199105cc908e874d74b"] - typeof values is String, so you operate with strings, while when you operate with object property(myObject.id) - you, probably, operate with ObjectID, which is default for latest MongoDBs. Anyway, we need to see your code and know your DB version etc. Easiest way to check is run code "typeof myObject.id", and if you see not "string" - this is an answer to your question.

Object property displayed in the console, but returns undefined when I read it

article is an object variable.
console.log("reesult id " + JSON.stringify(article));
When I do this, it outputs me: [{"Id":43}]
and when I do:
console.log(article[0]);
It outputs me {"Id":43}
but now... HOW to get just 43?
Because when I type:
console.log(article[0].Id);
, It returns me undefined instead of 43. Pf.
So, HOW to get 43?
It is very difficult because I made researches and it does not work as well.
I am unable to comment to so posting as an answer.
Your solution looks fine.
Could you print the output for
JSON.stringify(article[0].id)
Sequelize has it's own way how to return objects in json so you should try using their way to handle with objects since you are using that library.
This is from Sequelize documentation.
For any response object that resulted from a query, you can extract
only the data you want by appending .get({plain:true}) the the
response.
Here you have a link to that documentation so you can read more for that since you are not providing any code to us.

Javascript modify array objects property?

So I have an array, where I am trying to update an objects property value, like so:
this.tasks[5].status = "complete";
// Here is the object looks like when I log it:
{"rowID":16,"task":"and more stuff","status":"incomplete","inlist":"Homework"}
Yet the above code does not change the value, it does nothing. Is my syntax wrong, or is there something else in my code causing this?
It seems I can add properties to the object, but I cant modify existing ones. I also can delete the object, but cant delete a property from it.
Thanks
It must be something else in your code. Here are two pics of your setup working in both Chrome and IE9.
Chrome Console
IE9 Console

Categories

Resources