2017-05-26 25 views
1

我想讓最後添加的孩子在雲端功能。這是代碼。我想問是否有任何功能或查詢可用於獲取最後添加的孩子。 這是我的代碼:Firebase雲功能,獲取最後加入的孩子

exports.makeUppercase = functions.database.ref('aaa-fff/searchIndex') 
    .onWrite(event => { 
    // all records after the last continue to invoke this function 
    console.log(event.data.val()); 
    console.log(event.data.numChildren()); 
}); 

PS。我不想循環兒童。

+0

這是不是你所需要的東西,但我認爲它可以幫助你:HTTPS:/ /gist.github.com/MiroHibler/4740915 –

回答

0

我不能準確地確定你的用例是什麼,但是這將讓你的最後一個子:

exports.makeUppercase = functions.database.ref('aaa-fff/searchIndex') 
    .onWrite(event => { 
    // all records after the last continue to invoke this function 
    var lastChild; 
    event.data.forEach(function(child) { 
     lastChild = child; 
    }); 
    console.log(lastChild.val()); 
}); 
+0

我不想循環,如果孩子的數量是100萬,迭代次數將是100萬次。 –

+1

您的代碼在'/ aaa-fff/searchIndex'中被觸發,並且會收到所有'/ aaa-fff/searchIndex'。有了一百萬個子節點,循環將不會是昂貴的部分。如果你只想觸發新的子節點,你應該觸發一層更深的'aaa-fff/searchIndex/{pushid}'。有關此示例的文檔,請參閱此部分:https://firebase.google.com/docs/functions/database-events#handle_event_data –