while pushing an element for the first time to a child array which is null,I'm getting this error
"Cannot read property push of null"
But the element gets pushed,and the second time I do everything goes fine.It gets added to the array
this.group.departmentsList.push({
name: group.newCategoryName,
sortOrder: group.departmentsList.length,
type: "category"
});
group contains the data and departmentList is the child array which is declared like this:
$scope.parentDepartment = [
{
departmentsList: [{}]
}
];
Well don't push to nonexistent array maybe? You can explicitly check if it's not null and create one if needed:
this.group.departmentsList = this.group.departmentsList || [];
this.group.departmentsList.push({
name: group.newCategoryName,
sortOrder: group.departmentsList.length,
type: "category"
});
Related
In my code, I am creating a new object based on inputs from a JSON file. The JSON file has a lot of extraneous information, so I first extract the relevant parts into arrays and then use those arrays to make the new object, as a tree.
An example of the format I want the new object to be in is this: (much larger though, it will have uneven layers like below)
{
"name" : "flare",
"children" : [
{
"name" : "solar"
"name" : "photon"
}
]
"name" : "system",
"children" : [
{
"name" : "axios"
}
]
.....
}
The first layer is in an array called "packs" and the second children layer is in an array called "deps". (packLen and depsLen is also declared previously). My code is below:
let treeBe = {};
for (i=0; i<packLen; i++) {
treeBe.parent[i] = {name: packs[i]};
for (j=0; j<depsLen; j++) {
treeBe.parent[i].children[j] = {name: deps[i,j]}
}
}
I am getting an error "Cannot set property '0' of undefined", but that is appearing within the second loop. This object is based upon the array, which is based on the incoming JSON file. I do not know the size ahead of time, so I do not know how to define it ahead of time. Why is the second layer having the undefined problem and not the first?
Thank you in advanced.
EDIT:
I had to change these 3 lines of code:
let treeBe = {"parent" : [], "children" : []}
treeBe.parent[i] = {name: packs[i], "children": []}
treeBe.parent[i].children[j] = {name: deps[i,j]}
Solved: I had to change these 3 lines of code:
let treeBe = {"parent" : [], "children" : []}
treeBe.parent[i] = {name: packs[i], "children": []}
treeBe.parent[i].children[j] = {name: deps[i,j]}
I have a read-only object that is returned by GraphQL (vue-apollo) query, the result which is read-only looks something like this:
result: {
id: 'yh383hjjf',
regulations: [{ title: 'Test', approved: false}]
})
I want to bind this to a form and be able to edit/update the values in the regulations array and save it back to the database.
at the moment when I try to edit I get the error below:
Uncaught TypeError: "title" is read-only
I tried cloning the result returned by the database using object.assign
//target template
const regulatoryApprovals = {
id: null,
regulations: [{ title: null, approved: null}]
})
regulatoryApprovals = Object.assign(regulatoryApprovals, result, {
regulations: Object.assign(regulatoryApprovals.regulations, result.regulations)
})
but this didn't work.
Does anyone know how I can properly clone the result?
regulatoryApprovals= Object.assign(regulatoryApprovals, ... indicates the problem because regulatoryApprovals is modified with Object.assign, so it would need no assignment.
Read-only regulatoryApprovals object needs to be cloned. regulations is an array and won't be merged correctly with Object.assign, unless it's known that array elements need to be replaced. It should be:
regulatoryApprovals = {
...regulatoryApprovals,
...result,
regulations: [...regulatoryApprovals.regulations, result.regulations]
}
Where { ...regulatoryApprovals, ... } is a shortcut for Object.assign({}, regulatoryApprovals, ...).
I have an array of objects like
[
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
},
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
},
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
},
{
"id":17368,
"creationDate":1566802693000,
"status":"InProgress",
"type":"NEW",
"agentType":"Master"
}
]
But when trying to access the object property 'id' using console.log(array[0].id) throws a "cannot read property id of undefined error"
However just logging the first object with console.log(array[0]) prints the object successfully.
{id: 17368, creationDate: 1566802693000, …}
Also printing the list of ids using array.map(x => console.log(x.id)) prints the list of ids successfully .
I am in a situation where i need to access the first few specifically . Where am i going wrong ?
try this console.log(array[0] && array[0].id)
or you can use get from lodash-es like this:
import { get } from 'lodash-es'
const id=get(array[0], 'id', '')
I have the below JS code in my Ember app that gets called;
myPanels.accordionPanels = [];
myPanels.accordionPanels.push({
panel: {
name: "my-grid",
type: 'comp',
props: [{
key: 'elementId',
value: "myCustomId"
}]
}
});
So as you can see, I start by setting myPanels.accordionPanels = [] every time and then push the object.
However, I got the following error
Assertion Failed: Attempted to register a view with an id already in
use: myCustomId
So I am assuming that the object inside is not getting reset & it is able to find the earlier created "myCustomId".
Am I resetting the array (or rather the object inside it) correctly ?
Since I am able to push values using:
accordionPanels = [];
accordionPanels.push({
panel: {
name: "my-grid",
type: 'comp',
props: [{
key: 'elementId',
value: "myCustomId"
}]
}
});
make sure myPanels.accordionPanels doesn't have any prototype associated with it.
Try to inspect its value as:
myPanels.accordionPanels = [];
console.log(myPanels.accordionPanels); // see if it has values.
You can delete value using :
delete myPanels.accordionPanels PROTOTYPE
I need to access the "State" value from the following array --
data =
{
Images:
[
{
ProductCodes: [],
BlockDeviceMappings: [Object],
Tags: [],
ImageId: 'ami-75301c',
ImageLocation: '54696560/Test Image 3',
State: 'available',
VirtualizationType: 'pavirtul',
Hypervisor: 'xen'
}
],
requestId: '2eb809d3-7f82-4142-b5d1-6af3'
}
When I try data.Images["State"] or data.Images.State I get undefined.
Thanks
Images maps to an array which stores objects, so you have to specify the index of the item you want. Try data.images[0]["State"].
You can access like this:
data.Images[0].State
Or even:
data.Images[0]['State']
Access the state with data.image[0].state. Your method was wrong because inside the image, you need an index within the two square bracket, the image property is an array.