Reputation: 881
I am attempting to define a variable conn in my controller.js.
With the code at the bottom, I get lots of horrible UI glitches, so I'm presuming the code below is wrong. If I try:
var conn;
conn.name = 'Fred';
I get what looks like a NullPointer Error "Cannot set property 'name' of undefined" when I attempt to set conn.name.
How do I define my variable?
var addDBConnControllerBase = app.controller('addDBConnControllerBase',
function($scope,$http,serviceFactory,$modal,$location,$log)
{
$scope.saveDBConn = function()
{
var conn = {
name,
driverType,
connectionString,
userID,
password
};
conn.name = $scope.addDBConnController.displayName;
conn.driverType = $scope.addDBConnController.driverType;
conn.connectionString = $scope.addDBConnController.connectionString;
conn.userID = $scope.addDBConnController.userID;
conn.password = $scope.addDBConnController.password;
$log.debug('Saving db conn');
$log.debug(JSON.stringify(conn));
}
});
Upvotes: 2
Views: 33649
Reputation: 2935
You need to brush up on your javascript pronto! This:
var conn;
is a variable declaration, not definition. conn
is still undefined
after this statement so you can't go conn.name = ...
. You have to initialize things before you use them:
var conn = {};
conn.name = ...
or
var conn = {
name: ...
};
Upvotes: 4
Reputation: 301
You should define variable conn as an array first. Like following.
var conn = [];
conn.name=$scope.addDBConnController.displayName;
Upvotes: -1
Reputation: 1779
var newAttrs = {};
newAttrs[nameOfProp] = valueOfProp;
try this!!!
In your case I think this would be
var conn = {};
conn["name"] = 'Fred';
Upvotes: 4