简体   繁体   中英

Reference firebase post by key, pushes to “undefined”

What I am trying to do.

I am making an app that has posts and comments using react native and firebase. I am trying to add comments to it. I am trying to make it so that when you tap on a post, it takes you to another tab with the post, then the user can use a text input box to push a comment to firebase.

Problem

When the user pushes the text to firebase, instead of pushing to the post, it pushes to "undefined". Here is my code when the user presses the upload button:

firebase.database().ref('posts/'+params.key).push().set({ comment:this.state.commentInput });

"Comment input" being what is pushed, and "params.key" being the key of the post (I tested this and it is the correct key). I would love some help getting the comments to push to the correct place.

My suggestion is for you to validate the input before pushing it into firebase

 if (params.key === undefined && this.state.commentInput === undefined) {
     console.log('Wrong user input')
 } else {
    firebase.database().ref('posts').child(params.key).push().set({ comment:this.state.commentInput }).then(function (snapshot) {
        console.log('success push new value')
    }).catch(function (err) {
        console.log('any error: ', err);
    });
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM