簡體   English   中英

Firebase按鍵未定義

[英]Firebase Push Key Undefined

我希望在使用雲功能時從firebase push命令獲取密鑰。

const params = {
    date: Date.now(),
    movie,
    movieId: movie.id,
    userId: user.uid,
    group: 'home',
    type: 'watched'
};

const pastRef = event.data.adminRef.root.child(`pastActivity/${user.uid}`);

var newPostRef = pastRef.push().set(params);

var postId = newPostRef.key;

console.log(postId); //undefined

但是postId返回為undefined。 嘗試其他建議的方法但沒有任何結果。

Reference.set()返回一個無效承諾,該承諾在寫操作完成時解決。 您無法獲得它的鑰匙。 而是將push()set(...)拆分為單獨的語句,以便可以捕獲引用

var newPostRef = pastRef.push();
newPostRef.set(params);

var postId = newPostRef.key;

console.log(postId); //undefined

如果不需要newPostRef變量,可以使用較短的版本

//var newPostRef = pastRef.push().set(params);
//var postId = newPostRef.key;

const newPostRefKey = pastRef.push(params).key
//this pushes the data and returns the key

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM