When I want to use model like this:
console.log($scope.selectedMonth);
I get output:
Object { no: "02", name: "Veljača", $$hashKey: "object:24" }
But when I want to use one of its properties like this:
console.log($scope.selectedMonth.name);
I get error:
Error: $scope.selectedMonth is undefined
Why is that happening and how do I access model object properties?
When object was initialized it was undefined by default. This line of code is inside $watch method and I added if statement that fixed the problem.
if ($scope.selectedMonth !== undefined) {
console.log($scope.selectedMonth.name);
}
Related
I have an object with an array inside it. Object is returned from MongoDB.
{
brand: "OPEL",
createdAt: "2022-12-17T17:44:31.693Z",
image: [
{
public_id: 41658465461,
url: http://www.cloudinary.com,
},
],
licensePlate: "DR126",
mileage: 526,
model: "corsa",
qrCode: "#21566",
updatedAt: "2022-12-17T17:44:31.693Z",
_id: "639dffff33060ff5c3c796eb",
}
The problem is whenever I try to get the image url in my React component obj.image[0].url it returns an error TypeError: Cannot read properties of undefined (reading '0')
console.log(obj.image[0].url) returns undefined
After refreshing page returns 2 logs:
console.log(obj.image[0].url) returns undefined
console.log(obj.image[0].url) returns "http://www.cloudinary.com" (actual link)
I assume that object is not ready before I am trying to access it.
I looked over everyone and couldn't understand where is the issue and how can I solve it.
Thank you in advance for taking a look at this !
Tried forEach() loop, found 1 solution at stackoverflow didn't do the trick in my case.
change obj.image[0].url to obj?.image[0]?.url
the ? checks if the field exists and is not undefined then access to it
I have a React state which is array of objects and I am adding an object to it in setState method. The objects adds correctly.
Now when I try to access the object in some other function, I can't access the property of object. I can see on console that the object exists with the properties. I checked other similar questions on stackoverflow which are related to asynchronous calls but mine does not fall under that.
I have created array of object like this,
interface ResultState{
....
localArray : object[];
}
Initializing it to null in constructor.
And adding an object to it like,
localArray : this.state.localArray.concat(Items1)
My console output :
localArray -> [{}]
0:
name: 'abcd'
roll_num: 10
address: [{...}]
0: {Street: 'abcdefgh', aptnum: 1}
Now I want to access address of the object.
My code is like this,
const resultObject = this.state.localArray[0];
return resultObject.address;
But I get an error property address does not exist on type object.
when I do console.log(typeof(resultObject)), comes object.
what could possibly the reason for this?
I'm glad you got it working, but using type any[] isn't great because it says that your array could contain anything. What you really want is for typescript to know the type of the specific objects in your array. Based on what you've posted, it should be something like:
interface Address {
Street: string;
aptnum: number;
}
interface Entry {
name: string;
roll_num: number;
address: Address[];
}
interface ResultState {
....
localArray: Entry[];
}
That way typescript knows that your array elements are objects, but it also knows what properties are valid and what the types are for each property.
Yes, it was because of the type of the array. After changing it to localArray: any[], it worked!
I have a response from the server that looks like this
[
{
"metric":{
"id":"b66178b8-dc18-11e8-9f8b-f2801f1b9fd1",
"name":"Detector de fum bucatarie",
"metricValueType":"DOUBLE",
"metricType":"DEVICE_VALUE",
"groupUUID":null,
"projectUUID":null,
"companyUUID":"ccab28ed-3dcf-411f-8748-ec7740ae559c",
"unit":null,
"formula":null
},
"data":{
"1550150771819":"10.310835857351371"
}
}
]
Data property contains a hashMap with Timestamp and a Value.
When I try to get any value I recive this error:
myErrorTypeError: undefined is not an object (evaluating 'metricValues.metric.id')
How can I access a property ? I tried both methods :
metricValues.metric.id
and
metricValues["metric"]["id"]
How can I get the hashMap values?
I already tried this :
const timestamp = Object.keys(metricValues.data)[0];
const values = Object.values(metricValues.data)[0];
Your data is an array of objects. So even though your data has only one object, still it is an array of objects.
your object is at index 0 of the array.
You can access the metric Id as follows,
metricValues[0].metric.id
Try This (put that [0])
metricValues[0].metric.id
Your response from the server is an array. And the metric object is in the first element of that array.
To access the id inside the metric object you need to pass the index at which that object is present in the array which is 0 here.
That's why use :
metricValues[0].metric.id
I'm attempting to render the values of props object, When I console.log stats object, chrome dev tools console shows the following object.
Object
_id : "vFgY3YBrLj3Pd7zNA"
active: 652668928
available: 595034112
createdAt: Fri Jan 13 2017 03:56:04 GMT+0400 (GST)
free: 595034112
inactive: 563294208
ownerId: "6Kv93sJdY62iGiCwg"
percent : 86.1
However when I attempt to access .stats property I get undefined
console.log(this.props.stats._id);
Uncaught TypeError: Cannot read property '_id' of undefined
Any idea on what I'm going wrong here?
Check this jsfiddle, how to pass props values from parent component and use in child component : https://jsfiddle.net/r7ym0x4r/
I have noticed that this behavior happens because the object is not fully instantiated
In my case the query to fetch object data was slow, here is an example for fetching last record
Stats.findOne({}, {sort: {DateTime: -1, limit: 1}});
And when I changed it to something like, it worked fine.
Stats.find({}).fetch();
Im trying to remove a property element before doing the submission of that element.
$scope.changeTitle = function(song, title){
song.title = title;
delete song.label;
song.put();
}
When doing so it seems that the property "label" is removed. When I do the PUT operation the object actually has the property label.
// This is the object I'm sending (checked from the chrome dev tool - network)
{
artist: "XXXXX"
title: 'XX'
label: []
}
Is there any way to remove a property from an element?
If you inspect the object's put method in Developer console, you'll find that the referenced object is actually your original object (even after changes). In order fix the reference, use Restangular.copy() before you manipulate the object.
Earlier when you wrote something along the lines of:
$scope.song = restangular.one(song, 1).get().$object;
You should instead write:
$scope.song = restangular.copy(restangular.one(song, 1).get().$object);
To see the related issue, checkout: https://github.com/mgonto/restangular/issues/55
You can use underscore's _.omit function (http://underscorejs.org/#omit)
song = _.omit(song,'label');