Firebase:获取有关参加活动的与会者的更多信息

时间:2018-07-30 16:47:45

标签: javascript firebase asynchronous firebase-realtime-database

使用Firebase(Web),我试图从事件中获取有用的信息

我的数据库结构:

{
    users:
        "abc": {
            birthday: "21/07/1998",
            firstname: "Maxime",
            lastLogin: "30/07/2018"
        },
        "def": {
            birthday: "12/03/1973",
            firstname: "John",
            lastLogin: "12/04/2018"
        },
    },

    events: {
        "df16s5dfs21er6zf5f1ze-": {
            title: "Some great event",
            members: [
                "abc", 
                "def"
            ]
        } 
    }
}

我试图用Javascript获得的对象是一个包含所有事件,这些事件的成员以及有关这些成员的更多信息的对象。例如:

{
    "df16s5dfs21er6zf5f1ze-": {
        title: "Some great event",
        members: {
            "abc": {
                firstname: "Maxime",
                lastLogin: "30/07/2018"
            },
            "def": {
                firstname: "John",
                lastLogin: "12/04/2018"
            } 
        }
    } 
}

您可以看到,唯一添加的是应该直接从“用户”节点获取的“名字”和“ lastLogin”。

我用Javascript尝试了很多事情,但没有成功:(

有人能解决这种情况吗?

此刻我的代码:

let refEvents   = firebase.database().ref("/events");
let refUsers    = firebase.database().ref("/users");

refEvents
    .once('value')
    .then((snapEvents) => {
        snapEvents.forEach((snapEvent, kEvent) => {
            let event = snapEvent.val();
            // Iterate on event.members and write this info into event.members[key_member] = memberData;
        });
    });

编辑:

我尝试了以下操作,我想我只是缺少async / await

    refEvents
    .once('value')
    .then((snapshot) => {
        let events = snapshot.val();

        for(kEvent in events) {
            console.log(kEvent, "Is an event");
            let event = events[kEvent];

            for(member in event.members) { 
                console.log(member, "Is a member of event", kEvent);
                refUsers.child(member).child("/profile").once('value', (userProfile) => {
                    console.log("userProfile", userProfile);
                });
            }
        }

        console.log("final events", events);
        dispatch({ type: FETCH_EVENTS_S, payload: cleanNewEvents(snapshot.val()) });
    })
    .catch((err) => {
        dispatch({ type: FETCH_EVENTS_F, payload: err.message });
    });

这里的问题是执行循环后会出现“ userProfile”日志。

1 个答案:

答案 0 :(得分:0)

如何处理这种情况

获取事件

export const fetchEvents = async () => {
    let finalEvents = {};
    let events = await getEvents();
        events = events.val();

    for(kEvent in events) {
        let event   = events[kEvent];
        let members = await getMembers(event.members);

        event.members = members;

        finalEvents[kEvent] = event;
    }

    return finalEvents;
}

获取事件功能

const getEvents = () => {
    return firebase
        .database()
        .ref("events")
        .once("value");
}

获取一个活动的成员

const getMembers = async (members) => {
    let profiles = {};

    for(member in members) {
        let profile             = await getProfile(member);
        let { username , image } = profile.val();

        if(!image) image = "";

        profiles[member] = { username, image };
    }

    return profiles;
}

获取一名会员的个人资料

const getProfile = (kUser) => {
    return firebase
        .database()
        .ref("users")
        .child(kUser)
        .child("profile")
        .once("value");
}