首页 文章

firebase返回快照承诺

提问于
浏览
1

我正在使用firebase / firestore,我正在寻找一种方法来返回快照的承诺 .

onlineUsers(){
     // i want to return onSnapshot
    return this.status_database_ref.where('state','==','online').onSnapshot();
}

在我做的其他文件中

componentDidMount(){
    // this.unsubscribe = this.ref.where('state','==','online').onSnapshot(this.onCollectionUpdate) 
    firebaseService.onlineUsers().then(e=>{
        console.log(e)
    })
}

我得到了错误

错误:Query.onSnapshot失败:使用无效参数调用 . TypeError:_firebaseService2.default.unsubscribe不是函数

如果我这样做

onlineUsers(){
   return  this.status_database_ref.where('state','==','online').onSnapshot((querySnapshot)=>{
        return querySnapshot
    }) 
}

我明白了

TypeError: _firebaseService2.default.onlineUsers(...).then is not a function

另外,当我这样做的时候

this.unsubscribe = firebaseService.onlineUsers().then((querySnapshot)=>{
        console.log(querySnapshot.size)
        this.setState({count:querySnapshot.size})
    })

//其他文件

onlineUsers(callback) {
    return this.status_database_ref.where('state', '==', 'online').get()
}

它没有听到更改为firebase,意味着如果我更改firebase它不更新或更改大小..

---- firestore函数---我试图使firestore函数在每次UserStatus节点更新时触发,但这需要几秒钟而且对我来说很慢 .

module.exports.onUserStatusChanged = functions.database
.ref('/UserStatus/{uid}').onUpdate((change, context) => {
    // Get the data written to Realtime Database
    const eventStatus = change.after.val();

    // Then use other event data to create a reference to the
    // corresponding Firestore document.
    const userStatusFirestoreRef = firestore.doc(`UserStatus/${context.params.uid}`);


    // It is likely that the Realtime Database change that triggered
    // this event has already been overwritten by a fast change in
    // online / offline status, so we'll re-read the current data
    // and compare the timestamps.
    return change.after.ref.once("value").then((statusSnapshot) => {
        return statusSnapshot.val();
    }).then((status) => {
        console.log(status, eventStatus);
        // If the current timestamp for this data is newer than
        // the data that triggered this event, we exit this function.
        if (status.last_changed > eventStatus.last_changed) return status;

        // Otherwise, we convert the last_changed field to a Date
        eventStatus.last_changed = new Date(eventStatus.last_changed);

        // ... and write it to Firestore.
        //return userStatusFirestoreRef.set(eventStatus);
        return userStatusFirestoreRef.update(eventStatus);
    });
});

用于计算和更新在线用户数量的功能

module.exports.countOnlineUsers = functions.firestore.document('/UserStatus/{uid}').onWrite((change, context) => {

    const userOnlineCounterRef = firestore.doc('Counters/onlineUsersCounter');

    const docRef = firestore.collection('UserStatus').where('state', '==', 'online').get().then(e => {
        let count = e.size;
        return userOnlineCounterRef.update({ count })
    })
})

2 回答

  • 4

    JavaScript中的 Promise 可以解析(或拒绝)一次 . 另一方面, onSnapshot 可以多次给出结果 . 这就是 onSnapshot 没有回复承诺的原因 .

    在您当前的代码中,您将留下一个悬挂式的监听器 status_database_ref . 由于您不对数据执行任何操作,因此继续收听数据会很浪费 .

    而不是使用 onSnapshotuse get

    onlineUsers(callback){
        this.status_database_ref.where('state','==','online').get((querySnapshot)=>{
            callback(querySnapshot.size)
        }) 
    }
    

    或者用你原来的方法:

    onlineUsers(){
        return this.status_database_ref.where('state','==','online').get();
    }
    
  • -1

    我找到了一种方法

    onlineUsers(callback){
       return  this.status_database_ref.where('state','==','online').onSnapshot((querySnapshot)=>{
            callback(querySnapshot.size)
        }) 
    }
    
    componentDidMount(){
    
        this.unsubscribe = firebaseService.onlineUsers(this.onUpdateOnlineUsers);
        console.log(this.unsubscribe)
    }
    onUpdateOnlineUsers(count){
        this.setState({count})
    }
    componentWillUnmount(){
        this.unsubscribe();
    
    }
    

相关问题