Cannot assign to read only property 'x' of true [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm running into a frustrating issue in my Angular controller. I'm trying to conditionally set the values of certain fields of objects I attach to my scope. The if block below works perfectly by itself, but as soon as I add the else block I run into the following error:
TypeError: Cannot assign to read only property 'gameType' of true
var getGames = function() {
var defer = $q.defer();
playersService.getGames({
playerId: playerId
}).$promise.then(function(data) {
vm.games = data;
for (var x in vm.games) {
if (vm.games[x].activity_type === 'preseason') {
vm.games[x].gameType = 'preseason';
} else {
vm.games[x].gameType = vm.games[x].type;
}
}
defer.resolve(data);
});
return defer.promise;
};
After console.log'ing vm.games[x] within the else block I'm seeing the error occurs when I hit:
Promise {$$state: Object}
true
...whereas all the other lines show:
Resource {id: "...", ...}

Don't use a for in loop on array, use a standard for loop. for in is for iterating the properties of an object. This could be causing some hiccups when assigning property values.

Your vm.games[x].gameType data type and vm.games[x].type; data type may different.
if your if condition worked perfectly then you can try
if (vm.games[x].activity_type === 'preseason') {
vm.games[x].gameType = 'preseason';
} else {
vm.games[x].gameType = ''+ vm.games[x].type; // ''+ is convert to string
}

Related

Return object result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 26 days ago.
Improve this question
I need to get the result of this object, I've tried json.count(id_reported) and json['count(id_reported)'] but none worked.
const json = {
'count(id_reported)': 21
};
//console.log(json.count(id_reported));
console.log(json['count(id_reported)']);
In Javascript, Typescript and so in Express in the end, its easy to handle such things.
var myObject = {
'count': 21
}
myObject = JSON.parse(myObject);
console.log(myObject.count);
The JSON.parse is only needed, if you object is a string. Is it a Javascript object you do not need to parse.
The count(id_reported) part I don't understand. If your object looks like this in the end:
{
count(1): 1,
count(2): 2,
}
and you don't know the structure at all you can use a for loop:
for (let data in myObject) {
console.log(data); // data will be the key; so count(1) as example
}
See the in keyword in the for loop. This will give you the key. The on keyword otherwise gives the object in an array as example.

Set get of Object.defineProperty does not work properly undefined is printed [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Is it possible to manage object's properties via set get in defineProperty?
I'm not sure that I'm using this sentence properly.
<div id="app"></div>
why
<script>
var div = document.querySelector('#app');
var viewModel = {};
Object.defineProperty(viewModel, 'str' , {
get: function() {
return console.log("access");
},
set: function() {
return console.log("setting");
}
})
</script>
I assume that you have run viewModel.go in console - you will get "access" printed in console, but later you will get undefined as it is a result of this get function:
function() {
console.log("access");
}
This function doesn't have a return clause, so value of go will be undefined.

Array[key].push Is Not Defined in JavaScript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
When trying to push items to an array in javascripts it gives an error, the following pseudo code illustrate what is happening:
var data = new Array();
for(...) {
data[key].push(item[i]);
}
It is showing the following error:
Cannot read property 'push' of undefined
Thanks
If you need a 2d array, you have to initialize each element of the outer array to be an array.
// Have to check every array item, if it's not an array already, make it so
for(...) {
if (!data[key]) {
data[key] = [];
}
data[key].push(item[i]);
}
You could always do the following if you know the number of inner arrays you need:
var data = [[],[],[],[],[],[]];
In your example, since they variable name is key, I'm assuming you actually want an object of arrays. If you know the keys ahead of time, you can use the following literal.
var data = {
myKey1: [],
myKey2: []
}

how to assign values to undefined objects? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
var data = {};
data.info.id = "alpha";
This logs to the console: "TypeError: data.info is undefined".
Well that's great and all but I need to store a value in data.info.id. Isn't that what objects are supposed to do?
This should produce an object that looks like this:
data: {
info: {
id: "alpha"
}
}
Is data.info = {} really a necessary step?
In response to Patrick Evans - that's an unrelated question.
Well there is another way. That's putting the info-object directly in the data-object like this:
var data = {
info: {}
}
data.info.id = "alpha";
console.log(data);

How to add property to a global Javascript object inside a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.

Categories

Resources