Below is my code:
var obj = null;
var voterIC = "";
$(function ()
{
$("#btnRegister").click(function (e)
{
voterIC = $("#nric").val();
if ( voterIC == "")
$("#messageEdit").html("<h4 style=\"color:red\">Please fill in NRIC!</h4>");
else
obj = new Object();
obj.NRIC = voterIC;
obj.CreatedDate = firebase.database.ServerValue.TIMESTAMP;
var newPostKey = firebase.database().ref().child('voter').push().key;
var updates = {};
updates['/voter/' + newPostKey] = obj;
firebase.database().ref().update(updates).then(function()
{
$("#messageEdit").html('<h10 style=\"color:red\"><b>Update Successfully</h10><b>');
});
});
});
Why do I get the following error and how to solve this error?
Uncaught TypeError: Cannot set property 'NRIC' of null
Thank You.
elsebranch of yourif. Without them, only the first lineobj = new Object()is within the else branch, while the remainder is one level up. Hence the variableobjis not initialized at that point.