[英]Reference firebase post by key, pushes to “undefined”
我正在制作一個具有使用react native和firebase發布和評論的應用程序。 我正在嘗試添加評論。 我正在嘗試這樣做,以便當您點擊某個帖子時,它會將您帶到帶有該帖子的另一個選項卡,然后用戶可以使用文本輸入框將評論推送到firebase。
當用戶將文本推送到Firebase而不是推送到帖子時,它將推送到“未定義”。 這是用戶按下上傳按鈕時的代碼:
firebase.database().ref('posts/'+params.key).push().set({ comment:this.state.commentInput });
“ comment input”是要推送的內容,“ params.key”是發布的鍵(我對此進行了測試,它是正確的鍵)。 我希望獲得一些幫助,以將評論發送到正確的位置。
我的建議是讓您在將輸入推送到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);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.