set不是firebaseRef对象的函数

时间:2018-06-12 09:55:42

标签: javascript reactjs firebase firebase-realtime-database

这是Firebase文档中的示例

function writeUserData(userId, name, email, imageUrl) {
    firebase.database().ref('users/' + userId).set({
        username: name,
        email: email,
        profile_picture : imageUrl
    });
}

这是我的代码:

componentDidMount() {
    const questionsRef = firebase.database().ref('Works').orderByChild('firebaseKey').equalTo(this.props.questionId);
    console.log(this.props.questionId);
    questionsRef.on('value', snapshot => {
        let questions = snapshot.val();
        let newState = [];
        for (let question in questions) {
            newState.push({
                id: question,
                category: questions[question].category,
                level: questions[question].level,
                pointAmount: questions[question].pointAmount,
                pointBoost: questions[question].pointBoost,
                photoURL: questions[question].photoURL,
                workText: questions[question].workText,
            });
        }
        this.setState({
            questions: newState
        });
    });
    questionsRef.set({state: 'Taken', available: false});
}

现在,当我尝试运行它时,第一个查询工作正常,但questionsRef.set({state: 'Taken', available: false});只会抛出一个错误,说明:questionsRef.set is not a function。这是为什么?我错过了什么?

我的数据库结构:

"Works" : {
    "-LETUpQgZUUUhDfMo3XC" : {
        "available" : false,
        "boostAmount" : 8,
        "category" : "Fizyka",
        "createDate" : {
            "date" : 8,
            "day" : 5,
            "hours" : 10,
            "minutes" : 24,
            "month" : 5,
            "seconds" : 37,
            "time" : 1528446277672,
            "timezoneOffset" : -120,
            "year" : 118
        }
    },
    // More stuff
}

1 个答案:

答案 0 :(得分:0)

好的,这就是我解决它的方法。

var currentQuestion = firebase.database().ref('Works').child(this.props.questionId);
currentQuestion.child('available').set(false);