[我正在使用.on('value')
在Firebase节点上设置一个侦听器,然后一旦数据库使用.then()
两次添加了值,就尝试链接多个promise。
但是我遇到错误firebase.database().ref().on(...., *) 'callback' must be a function
关于如何解决此问题的想法?
database()
.ref('profiles/users/' + user.uid)
.on('value')
.then(snapshot => {
console.log(snapshot);
})
.then(() => {
console.log('Whatever');
});
'on'侦听器会在设置了侦听器的数据发生更改时使用回调函数,这就是您未提供回调的原因而导致的错误所在。您可以轻松地在该回调中完成任何数据操作,而无需链接一堆'.then'。这是在数据更改后设置状态的示例。
import React, { useEffect, useState } from 'react';
import database from '@react-native-firebase/database';
function User({ userId }) {
const [user, setUser] = useState(null);
useEffect(() => {
const subscriber = database();
subscriber.ref(`/users/${userId}`).on('value', snapshot => {
setUser(snapshot.val());
//or call another function that will do the rest of the async tasks/data manipulation
});
// Stop listening for updates when no longer required
return () => subscriber();
}, [userId]);
// return whatever here
}