Reputation: 53
I have a user structure where I can create and update a user but I cannot remove a user due to a reference child error due to some kind of string.
Basically, I have a realtime database consists of different users ID then there's the Name, Age and Nationlaity.
I want to be able to try and delete a user by its user ID for instance (usr5) so that when I enter that it'll delete the user from the database.
Can anyone help?
The Error:
Reference.child failed: First argument was an invalid path = "[object HTMLInputElement]". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
at fi (validation.ts:382)
at o.child (Reference.ts:90)
at removeUser ((fie path folders) index.html:115:58)
My HTML:
<body>
<div>
<h4>Create User: </h4>
<form>
<label>Create User ID: </label><input type="text" id="usrID"/><br/>
<label>First Name: </label><input type="text" id="usrFName"/><br/>
<label>Age: </label><input type="number" id="usrAge"/><br/>
<label>Nationality: </label>
<select id="usrNation">
<option value="American">American</option>
<option value="British">British</option>
<option value="French">French</option>
</select>
<button id="createUserBtn">Create</button>
</form>
</div>
<div>
<h4>Remove User: </h4>
<form>
<label>Create User ID: </label><input type="text" id="usrID"/><br/>
<button id="removeUserBtn">Remove</button>
</form>
</div>
My Firebase/Javascript:
<script>
var database = new Firebase("https://(my db url)/"),
userdb = database.child("/users");
userID = document.getElementById('usrID'),
userFirstName = document.getElementById('usrFName'),
userAge = document.getElementById('usrAge'),
userNationality = document.getElementById('usrNation');
function createUser(userID, usrFName, usrAge, usrNation){
firebase.database().ref('users/' + userID).set({
usrFName: usrFName,
usrAge: usrAge,
usrNation: usrNation
});
console.log('Success');
}
function insertAutoKey(usrFName, usrAge, usrNation){
var newPost = {
usrFName: usrFName,
usrAge: usrAge,
usrNation: usrNation
};
var newPostKey = firebase.database().ref().child('users').push().key;
var updates = {};
updates['/users/' + newPostKey] = newPost;
return firebase.database().ref().update(updates);
}
document.getElementById('createUserBtn').addEventListener('click', function(){
var usrID = userID.value,
usrFName = userFirstName.value,
usrAge = userAge.value,
usrNation = userNationality.value;
if(usrID && usrFName && usrAge && usrNation) {
createUser(usrID, usrFName, usrAge, usrNation);
}
});
function removeUser(){
var userRef = firebase.database().ref().child("users").child(userID);
userRef.once('value', function(snapshot) {
if (snapshot.val() === null) {
alert("no user found");
}else{
userRef.remove();
}
});
console.log('Remove Success');
}
document.getElementById('removeUserBtn').addEventListener('click', function(){
removeUser();
});
Upvotes: 1
Views: 263
Reputation: 80952
Change this:
userID = document.getElementById('usrID'),
userFirstName = document.getElementById('usrFName'),
userAge = document.getElementById('usrAge'),
userNationality = document.getElementById('usrNation');
into this:
userID = document.getElementById('usrID').value;
userFirstName = document.getElementById('usrFName').value;
userAge = document.getElementById('usrAge').value;
userNationality = document.getElementById('usrNation').value;
You need to add the property value
to retrieve the value of each element.
Upvotes: 1