我使用以下代码为 Firebase 数据库引用设置监听器:
export function listenToUserEventsFeed (userId, cb, errorCB) {
database.ref(`proUserEvents/${userId}`).on('value', (snapshot) => {
console.log('SNAPSHOT RECEIVED')
const feed = snapshot.val() || {}
const sortedIds = Object.keys(feed).sort((a, b) => feed[b].createdAtTimeStamp - feed[a].createdAtTimeStamp)
cb({feed, sortedIds})
}, (error) => {
console.log('SNAPSHOT ERROR: ', error)
})
}
但是,如果我在没有互联网连接的情况下进行测试,则 console.log('SNAPSHOT ERROR: ', error)
永远不会运行。我是否遗漏了什么或者我的代码有问题?我本质上想将错误传递给 errorCB() 函数。
最佳答案
只有在发生错误时才会调用错误回调,即当您当前的客户端无权读取它正在尝试读取的数据时。
没有互联网连接并不是错误。
如果你想detect whether there is an internet connection ,监听 .info/connected
。
相关:
关于javascript - 当没有互联网连接时,错误回调不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39715358/