简体   繁体   English

如何正确运行 firebase 功能?

[英]How to run a firebase function correctly?

This function will return the value of initiate_session, initiate_session[user_session].events, user_session before the firebase function runs.此函数将在initiate_session, initiate_session[user_session].events, user_session函数运行之前返回initiate_session, initiate_session[user_session].events, user_session的值。

How to run the firebase function first ??如何先运行 firebase 功能?

function createSession() {
var user_session = randomCharacters(20) //8iKhA4Aq2!6gZ)890ip@;
var ip = '127.0.0.1';
var initiate_session = new Object();
var session_started = new Date().getTime();


firebase.database().ref(user_session).once('value', function (snapshot) {
    if (snapshot.exists()) {
        console.log('session exists');
        createSession();
    } else {
        console.log('session not exists')

        initiate_session[user_session] = {
            ip: ip,
            session_started: session_started,
            events: {}
        };

        firebase.database().ref(user_session).set({
            ip: ip,
            session_started: session_started,
            events: {}
        });
    }
});

console.log('new session', initiate_session);
return [initiate_session, initiate_session[user_session].events, user_session];}

This is because the Firebase function is asynchronous - the code inside the method that gets the snapshot is a callback that is executed after the Firebase read is finished, which may take several seconds.这是因为 Firebase 函数是异步的 - 获取快照的方法内部的代码是在 Firebase 读取完成后执行的回调,这可能需要几秒钟。 However, as soon as you dispatch the read request via .once(...) your execution flow continues and the return is called.但是,一旦您通过.once(...)分派读取请求,您的执行流程就会继续并调用返回。

There are a few possible solutions:有几种可能的解决方案:

  1. Pass a callback argument to your createSession() method that is called with the values you are trying to return, instead of returning them directly.将回调参数传递给使用您尝试返回的值调用的createSession()方法,而不是直接返回它们。

  2. Return a promise from your method that resolves to give the values you're trying to return.从您的方法返回一个承诺,该承诺解析为您尝试返回的值。

  3. Use async / await syntax for your Firebase call.对 Firebase 调用使用async / await语法。 This is covered already in this question: running queries in firebase using async / await这个问题已经涵盖了这一点: 使用 async / await 在 firebase 中运行查询

Rough Example of #1 #1 的粗略示例

function createSession(onSessionCreated) {
    var user_session = "whatever";
    var initiate_session = new Object();

    firebase.database().ref(user_session).once('value', function (snapshot) {
        // do things with the snapshot
        onSessionCreated(initiate_session, initiate_session[user_session].events, user_session)
    });
}

// usage:
createSession(function (initiate_session, events, user_session) {
    // do things with initiate_session, events and user_session
});

Rough Example of #2 #2 的粗略示例

function createSession() {
    var user_session = "whatever";
    var initiate_session = new Object();

    firebase.database().ref(user_session).once('value').then(function (snapshot) {
        // do things with the snapshot
        return [initiate_session, initiate_session[user_session].events, user_session];
    });
}

// usage:
createSession().then(function (results) {
    // do things with results (i.e. the three elements in the array above)
});

Rough Example of 3 3 的粗略示例

async function createSession() {
    var user_session = "whatever";
    var initiate_session = new Object();

    const snapshot = await firebase.database().ref(user_session).once('value');
    // do things with the snapshot

    return [initiate_session, initiate_session[user_session].events, user_session];
}

// usage:
const results = await createSession();

If you're new to async / await code it probably won't be the easiest place to start as it may require changes elsewhere in your code, but this article is a good resource if you're keen to learn.如果您不async / await代码,它可能不是最容易开始的地方,因为它可能需要在代码的其他地方进行更改,但如果您渴望学习,这篇文章是一个很好的资源。

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

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