因儿童参考错误而无法删除数据?

时间:2019-03-31 10:36:58

标签: javascript html firebase firebase-realtime-database

我有一个用户结构,可以在其中创建和更新用户,但是由于某种字符串的引用子错误而无法删除用户。

基本上,我有一个由不同用户ID组成的实时数据库,然后是姓名,年龄和国籍。

It clearly that I have download the module.

HTML Error

我希望能够尝试通过其用户ID(例如,用户名(usr5))删除用户,以便在我输入该用户ID时将其从数据库中删除。

有人可以帮忙吗?

错误:

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)

我的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>

我的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();
});

1 个答案:

答案 0 :(得分:1)

更改此:

userID          = document.getElementById('usrID'),
userFirstName   = document.getElementById('usrFName'),
userAge         = document.getElementById('usrAge'),
userNationality = document.getElementById('usrNation');

对此:

userID          = document.getElementById('usrID').value;
userFirstName   = document.getElementById('usrFName').value;
userAge         = document.getElementById('usrAge').value;
userNationality = document.getElementById('usrNation').value;

您需要添加属性value来检索每个元素的值。