Skip to content

Instantly share code, notes, and snippets.

@Luiyit
Last active December 17, 2024 15:25
Show Gist options
  • Save Luiyit/43ad2106ebf22dcf944a4f1a0407448a to your computer and use it in GitHub Desktop.
Save Luiyit/43ad2106ebf22dcf944a4f1a0407448a to your computer and use it in GitHub Desktop.
React Native database ref set promise never resolves

Packages

"@react-native-firebase/app": "^18.8.0",
"@react-native-firebase/auth": "^18.8.0",
"@react-native-firebase/database": "^18.8.0",

How to replicate it?

import database from '@react-native-firebase/database';

const customHook = () => {
  useEffect(() => {
    // Set promise never resolves!!
    database().ref('/live_users/<USER_ID>')
      .set({ date: new Date() })
      .them(() => console.log("success"))
      .catch(() => console.log("error"))
  },[]);
}

How to solve it?

You need to get the database reference from the app.

import { firebase } from '@react-native-firebase/database';

const customHook = () => {
  useEffect(() => {
    // Now set call should work
    firebase
      .app()
      .database()
      .ref('/live_users/<USER_ID>')
      .set({ date: new Date() })
      .them(() => console.log("success"))
      .catch(() => console.log("error"))
  },[]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment