Error when bind values into a nested object - angularjs

Problem when binding values into a nested object
I want to add data to the following object structure.
Company {
stat {
internalData {
value = 35
}
}
}
I have used the below code
Company.stat.internalData["value"] = 35;
When I used the above code I got the error as internalData is undefined.
Could someone kindly help me. Thanks inadvance.

It seems that you have already created the following object:
let Company = {
stat: {
}
}
As you can see, internalData is sure not defined. You can't access it's fields using internalData["value"]. You need to create it first like this:
Company.stat.internalData = {};
And then define its property called value:
Company.stat.internalData["value"] = 35;
Company.stat.internalData.value = 35; would work too.
Or, you can create the entire object with just one expression:
let Company = {
stat: {
internalData: {
value: 35
}
}
}

Issue here is you are trying to access the property before the object Company is defined. So you need to define something like this before setting the value,
Company = {};
Company.stat = {};
Company.stat.internalData["value"] = 35;

Related

CanJS - Incorrect list objects type after mapping

If I have this instantiation const someObj = new MyObj({...someParams}) and MyObj is defined this way:
MyObj = DefineMap.extend("MyObj", {
myProp: {
Type: AnotherType.List
}
})
and
AnotherType.List = CanList.extend("AnotherType.List", {
Map: AnotherType
}, {
serialize() {
const res = [];
for (let i = 0; i < this.attr("length"); i++) {
res.push(this.attr(i.toString()).serialize());
}
return res;
}
})
then the someObj.myProp[0] type is DefineMap instead of AnotherType. How can I fix this?
I already tried this but still not working: const someObj = canReflect.assignDeep(new MyObj(), {...someParams})
With the help of Bitovi community I solved it (thanks to Brad Momberger).
So Brad explained that it happens because of the DefineList's bubble binding and the same doesn't happen with DefineMap objects.
So we always need to serialize the lists first the get the correct type of the list entries at the end, like this:
const someObj = new MyObj({
...someParams,
myProp: someParams.myProp.serialize()
})
You can see the full explanation here.

AWS GraphQL JSON string formatting

I'm attempting to create an object value to pass into DynamoDB using AWS AppSync and GraphQL. I'm very close to what I need but I'm stumbling on nested JSON.
Let's say I have an array:
let officers = [{"id":"0","IgRole":"Role1","IgName":"testname1","IgEmail":"testemail1","IgPhone":"testphone1","IgStart":"teststart1","IgEnd":"testend1"},
{"id":"1","IgRole":"Role2","IgName":"testname2","IgEmail":"testemail2","IgPhone":"testphone2","IgStart":"teststart2","IgEnd":"testend2"}]
I now want to create an object with each of the array values as a child object so, I do this:
for (let i in officers) {
officersJson['"' + officers[i].IgRole + '"'] = '{"Name":"' + officers[i].IgName + '","Email":"' + officers[i].IgEmail + '","Phone":"' + officers[i].IgPhone + '","Date commenced":"' + officers[i].IgStart + '","Date to end":"' + officers[i].IgEnd + '"}';
}
Here are the results:
Object {
"Role1": "{'Name':'testname1','Email':'testemail1','Phone':'testphone1','Date commenced':'teststart1','Date to end':'testend1'}",
"Role2": "{'Name':'testname2','Email':'testemail2','Phone':'testphone2','Date commenced':'teststart2','Date to end':'testend2'}"
}
I think the problem is that the each entire key / value is not in string format. If you look at
"Role1": "{......
you can see that the string breaks.
and this is the response from AWS:
Variable 'Officers' has an invalid value. Unable to parse {\"Role1\"={\"Nam
See the = sign
How can I format the object into a complete JSON string? I was fairly pleased I managed to get anywhere near the format I needed but this last bit has me stumped.
Finally worked it out. I needed a slightly different approach. Posting it in case it helps anyone else:
Firstly I created an array of roles, as this will be the keys for the value objects:
for (let i in officers) {
roles.push(officers[i].IgRole);
}
I then created a new array.
let arrOfficers = [];
for (let i in officers) {
arrOfficers.push(officers[i]);
}
I then use a function to create objects from an array:
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
let key = obj[property];
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(obj);
return acc;
}, {});
}
let newresult = groupBy(arrOfficers, "IgRole");
Finally I create my object and then stringify it:
let officersJson = {};
for (let i in roles) {
officersJson[roles[i]] = newresult[roles[i]][0];
}
theObjectIwant = JSON.stringify(officersJson)

How to stop original list from updating when cloned list is updated

I want to stop self.lineDetails from being updated when I update self.modifiedLineDetails.
self.modifiedLineDetails = [];
angular.forEach(self.lineDetails, function (value1, index1) {
var lineDetail = self.lineDetails[index1];
self.modifiedLineDetails.push(lineDetail)
});
console.log(self.lineDetails)
angular.forEach(self.modifiedLineDetails, function (value10, index10) {
var modifiedLineDetail = self.modifiedLineDetails[index10];
if (modifiedLineDetail.SelectedCustomers.length > 0) {
modifiedLineDetail.SelectedCustomers = 1;
} else {
modifiedLineDetail.SelectedCustomers = 0
}
});
console.log(self.modifiedLineDetails)
Previously I just assigned it like this self.modifiedLineDetails = self.lineDetails then I updated self.modifiedLineDetails but it wasn't working so I tried pushing it per line but self.lineDetails keeps updating.
You should clone an array and then modify your new array, one way to do this is using
spread operation ...
Here is the example of it:
var initialarray = [1,2,3,4];
var modification = [...initialarray];
modification.push(5);
console.log(initialarray);
console.log(modification);
Since the problem seems to be occurring because I was using angular. I researched and found angular.copy which creates a deep copy. The code below worked for me.
self.modifiedLineDetails = angular.copy(self.lineDetails);

Create objects on Ionic

Good morning!
I've been working with Ionic for a few weeks and I thought I actually understood how typescript and angular works, but I've found myself with a weird trouble that I think it will be very silly...
I'm trying to create an object called pendingWaybills with some properties named waybills, clients and clientWaybills. The thing is that I'm creating it this way:
pendingWaybills: {
waybills: any
clients: any,
clientWaybills: any,
};
I've also tried
pendingWaybills: {
"waybills": any,
"clients": any,
"clientWaybills": any,
};
And some other ways, but when I try to assign a value to this properties I'm getting the following error: TypeError: Cannot set property 'waybills' of undefined
I've also tried to assign some string or integers just to see if it was about the data that I was trying to assign like this.pendingWaybills.waybills = "Hi"; but I'm still getting the same error...
Would be glad to get some help as I think it's all about the way to create the object (and I also think it will be very silly) but I'm so stuck here.
Thank you!
Edit:
Here is where I try to assign the data to the object. (The variable data is a json)
loadPendingWaybills(){
this.loadClients(2)
.then(data => {
this.pendingWaybills.waybills = data;
var preClients = this.pendingWaybills.waybills;
this.clients = [];
for(let i = 0;i < preClients.length; i++){
if(this.pendingWaybills.clients.indexOf(preClients[i].descr1_sped) == -1){
this.pendingWaybills.clients.push(preClients[i].descr1_sped)
}
}
this.pendingWaybills.clientWaybills = [];
for(let i = 0; i < this.pendingWaybills.clients.length; i++){
this.getWaybills(this.pendingWaybills.clients[i], 2)
.then(data => {
if(this.pendingWaybills.clientWaybills[i] != data){
this.pendingWaybills.clientWaybills[i] = data;
}
});
}
});
}
You need to create an empty instance of the object, declaring the properties doesn't create the variable, it only tells your ide which properties it has:
public pendingWaybills = {
waybills: []
clients: [],
clientWaybills: [],
};
In Typescript, doing :
pendingWaybills: {
waybills: any;
clients: any;
clientWaybills: any;
};
will only set pendingWaybills variable type. To declare and assign value to the variable, you must do something like :
pendingWaybills = { // note the "="
waybills: something,
clients: something,
clientWaybills: something,
};
Put this in constructor
constructor() {
this.pendingWaybills = {
waybills: [],
clients: [],
clientWaybills: [],
};
}
Some explanation
It is nested object you can not create right away when you declare it.
For example var xxx = 'hi'; is fine. but if you do var xxx.yyy = 'hi' is not fine, as xxx is not defined before so yyy of xxx will cause error.
You can do
var xxx = {
yyy: 'hi'
};
or you can do
var xxx = {};
xxx.yyy = 'hi';

Angularjs: Assigning Array within object

I am having an issue with losing data within an array when i try to assign it to a new array.
My object im using is as follows:
$scope.shops = [
{
name: "Kroger",
items: [ { itemName: "Chips"} ]
}
];
This is the code for the functions im using, it may be a callback issue? or something? Im losing the items info for the shop.
$scope.addItem = function(newItem, newShop){
var x = findShop(newShop);
x.items.push(newItem);
$scope.shops.push(x);
};
findShop = function(shopTag){
var old = angular.copy($scope.shops);
var tar = {
name: shopTag,
items: []
};
$scope.shops = [];
angular.forEach(old, function(shop, key){
if(shop.name === shopTag) {
tar.items = angular.copy(shop.items);
}
else {
$scope.shops.push(shop);
}
});
return tar;
};
the goal is to have the findShop function return a shop with the correct name, with empty items if there wasnt a shop previously, or with items full of the items if the shop was already created. then the addItem will push the item into the shop.items array and push the shop into the $scope
Any help is greatly appreciated!!!
You are right , it is this line which is causing the problem ,
tar.items = shop.items;
Try using it like this ,
tar.items = angular.copy(shop.items);
var old = $scope.shops; // old and $scope.shops point to the same place
..........
$scope.shops = []; // you assigned a new array that overrides the data
............
angular.forEach(old, function(shop, key){ // for each on an empty array????
If you dont want to point to the same reference use:
var copiedObject = angular.copy(objToCopy);
I guess the array is getting empty even before for loop.
Var old is reference to shops array, which you are making empty before foreach.. effectively making old empty...

Resources