简体   繁体   English

Firebase将集合作为数组返回

[英]Firebase return a collection as an array

I am trying to build an app using Firebase as the back end and need help with what seems like a simple problem. 我正在尝试使用Firebase作为后端来构建应用程序,并且需要帮助看似简单的问题。 I want to return a list of nested data (I know I shouldn't be nesting data!) as an array. 我想返回一个嵌套数据列表(我知道我不应该嵌套数据!)作为数组。 To try and solve this I have the following to give me a collection of the status for each record in the database: 为了尝试解决这个问题,我可以通过以下方法收集数据库中每个记录的状态:

    var ref = new Firebase("https://firebase_URL");
ref.orderByChild("Status").on("child_added", function (snapshot) {
    var articleID = snapshot.key();
    var articleData = snapshot.val();
    console.log(articleData.status.Status);

This gives me a collection of the Status but how do i combine these to console.log the list as an array? 这给了我状态的集合,但是我如何将它们组合到console.log列表中作为数组?

You can declare a array outside the scope of the firebase event, and on the event you push to it: 您可以在firebase事件范围之外声明一个数组,并在该事件上推送到它:

var ref = new Firebase("https://firebase_URL");
var statusCollection = []; //Array outside the 'child_added' scope
ref.orderByChild("Status").on("child_added", function (snapshot) {
    var articleID = snapshot.key();
    var articleData = snapshot.val();
    statusCollection.push(articleData.status.Status);
    //Console shows the full array collection
    console.log(statusCollection)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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