I am trying to access a property of an object, I know the object exists (and has the property I need) as i can log it to the console.
However when i try and access it, it says Cannot read property 'price' of null.
I am a C# developer and little confused I must be doing something wrong but i do not know what? From my screenshot, you can see the object returned.
My code is below:
self.total = ko.computed(function(){
var total = 0;
var selectedServerName = this.selectedServer();
var selectedServerObject = ko.utils.arrayFirst(this.server, function(server){
console.log("server.name = " + server.name);
console.log("selectedServerName = " + selectedServerName);
var serverCompare = (server.name == this.selectedServer());
console.log("serverCompare is " + serverCompare);
}, this);
var selectedOsName = this.selectedOs();
var selectedOsObject = ko.utils.arrayFirst(this.os, function(os){
console.log("os.name = " + os.name);
console.log("selectedOsName = " + selectedOsName);
var osCompare = (os.name == this.selectedServer());
console.log("osCompare is " + osCompare);
}, this);
total = total + selectedServerObject.price + selectedOsObject.price;
return total;
},this);
Update
My updated fiddle is here: fiddle here
Thanks to d.raev it appears when the second log line is added console.log(selectedServerObject.price
, the arrayFirst function starts being called with selectedServer = undefined
.
server.name = DELL R210
selectedServerName = undefined
serverCompare is false
server.name = DELL R710
selectedServerName = undefined
serverCompare is false
server.name = DELL R720 Dual CPU
selectedServerName = undefined
serverCompare is false
os.name = Windows Standard
selectedOsName = undefined
osCompare is false
os.name = Windows Enterprise
selectedOsName = undefined
osCompare is false
os.name = CentOS Linux
selectedOsName = undefined
osCompare is false
os.name = Debian
selectedOsName = undefined
osCompare is false
1. Uncaught TypeError: Cannot read property 'price' of null
The undefined value from the selectedOsName or selectedServerName is causing the total to fail, what is the reason they are being set as undefined?