简体   繁体   English

当 firebase 函数在另一个文件中时,如何保持它打开

[英]How can I keep a firebase function open when it's in another file

I have a file called db.js from which I make all my firebase calls.我有一个名为 db.js 的文件,我从中进行了所有的 firebase 调用。

I am calling a function in db.js from another file called home.js How do I make it that the firebase connection stays open and the data gets passed back to home.js?我正在从另一个名为 home.js 的文件中调用 db.js 中的一个函数如何使 firebase 连接保持打开状态并将数据传递回 home.js? I can't use a promise because that closes the connection.我不能使用承诺,因为这会关闭连接。

Here is the function from db.js:这是来自 db.js 的函数:

export function getShopNames() {
    let userID = auth.currentUser.uid
    let stores = []
    userDB.ref('/users/' + userID + "/stores").on('value', snap => {
        snap.forEach(storeNames => {
            stores.push(storeNames.key)
        })
        return stores
    })
}

and I call it from home like this:我在家里这样称呼它:

let stores = db.getShopNames()

I want it to work so if a new store gets added to the real-time database, the variable updates我希望它能够工作,所以如果一个新商店被添加到实时数据库中,变量就会更新

There is no concept of file based scope in JavaScript. JavaScript 中没有基于文件的范围的概念。 The listener will stay active from the moment you call on('value' , until you either call off on that same location or until you load a new page.从您调用on('value'的那一刻起,侦听器将保持活动状态,直到您在同一位置调用off或加载新页面为止。

But your return stores doesn't do anything meaningful right now.但是您的return stores现在没有做任何有意义的事情。 It returns a value from the callback function that nobody will ever see/use.它从回调函数中返回一个没人会看到/使用的值。

Data is loaded from Firebase asynchronously, which means you can't return it from a function in the normal way.数据是从 Firebase 异步加载的,这意味着您无法以正常方式从函数返回它。 By the time the return statement runs, the data hasn't loaded yet.到 return 语句运行时,数据尚未加载。 That's why you'll usually return a so-called promise, which then resolves when the data has loaded.这就是为什么你通常会返回一个所谓的承诺,然后在数据加载时解析。

In your function that'd be:在您的功能中,这将是:

export function getShopNames() {
  return new Promise(function(resolve, reject) {
    let userID = auth.currentUser.uid
    let stores = []
    userDB.ref('/users/' + userID + "/stores").once('value', snap => {
        snap.forEach(storeNames => {
            stores.push(storeNames.key)
        })
        resolve(stores);
    }, (error) => {
        reject(error);
    })
}

Now you can call this function like this:现在你可以像这样调用这个函数:

getShopNames().then((shopnames) => {
  console.log(shopnames);
})

Update : you commented that you also want to handle updates to the shop names, you can't use once() and can't use promises (since those only resolve once).更新:您评论说您还想处理商店名称的更新,您不能使用once()并且不能使用承诺(因为那些只能解决一次)。

Instead pass in a custom callback, and invoke that every time the shop names change:而是传入自定义回调,并在每次商店名称更改时调用它:

export function getShopNames(callback) {
    let userID = auth.currentUser.uid
    let stores = []
    userDB.ref('/users/' + userID + "/stores").once('value', snap => {
        snap.forEach(storeNames => {
            stores.push(storeNames.key)
        })
        callback(stores);
    })
}

And then call it like:然后像这样调用它:

getShopnames(function(shopnames) {
    console.log(shopnames);
});

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

相关问题 重新加载页面后,如何保持所选标签页处于打开状态 - How can I keep the selected tabs open when page is reloaded 当 go 到另一个页面时,如何将数据表 state 保留到另一个页面? - How can I keep the datatable state when go to another page? 使用Firebase,如何将创建的密钥传递给另一个函数? - Using Firebase, how can I pass a created key to another function? 当我进入函数时,如何保持`this`的值? - How can I keep the value of `this` when I go inside a function? 如何将 window.onload 中的函数输出传递给另一个 javascript 文件? - How can I pass a function output that's inside of window.onload to another javascript file? 将变量传递给函数时,如何跟踪变量的值? - How do I keep track of a variable's value when it's passed into a function? 当另一个 function 被执行时,我如何打破 function? - How can i break a function when another function is executed? 单击该菜单内的锚链接时,如何使焦点菜单保持打开状态? - How can I keep the focus menu open when clicking on an anchor link inside that menu? 如何在页面加载时保持手风琴面板打开,但之后保持其可折叠性? - How can I keep my accordion panels open when the page loads, but maintain its collapsibility afterwards? 如何使用在另一个文件中定义的功能? - How can i use a function i defined in another file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM