[英]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.