React-native-firebase: [WEB SDK COMPAT] await .push() returns null, but works on web sdk

Created on 19 Mar 2018  路  7Comments  路  Source: invertase/react-native-firebase

Issue

Given the code:

const messageRef = await firebase.database().ref('xxx/123/messages').push(message)
// messageRef = null

If I use await it returns null. But on web sdk it works just fine.
Just converted from firebase to react-native-firebase and faced this compatibility problem.
If I don't use await it works fine, but it should match the web.

Related but closed: #147, #696

Environment

iOS (not tested on Android)

  1. Application Target Platform: macOS High Sierra
  1. Development Operating System: iOS 11
  1. Build Tools: ?
  1. React Native version: 0.54
  1. RNFirebase Version: 3.3.1
  1. Firebase Module: Database
Needs Tests Bug Web Implementation JavaScript Database

Most helpful comment

@simonbengtsson awesome, thanks for that - looks like await support on push() is missing on RNFB - will get it fixed.

All 7 comments

@Salakar Anything new on this issue, still does not work for me.

const ref = firebase.database().ref(/users/${uid}/points).push({ name: propValues.name, address: propValues.address, number: propValues.number, }) .then(() => { const key = ref.key; console.log(key); }) .catch((error) => { console.log(error); });

This should give me the key value, but is empty.

Hmmm It actually worked without the then, so the problem is solved in my case.

Just thought that the reference key was based on what the promise returned, so I was using the 'then' to wait for that promise.

This is the answer:
const ref = firebase.database().ref(dbref).push({ name: propValues.name, address: propValues.address, }); console.log(ref.key);

@Friis1978 without the await is also correct as creating a new reference + id path via push() is synchronous/client sided, though I believe the web SDK supports await as well like you mentioned above so we still need to add support for that our end.

Are you able to test the following 3 scenarios on the Web SDK if you still have it handy and report back with the logs if possible, would be super helpful:

// 1
const ref1 = firebase.database().ref(dbref).push();
console.log('1a', ref1.key); // should be key ?
const result = await ref1.set({ name: propValues.name, address: propValues.address });
console.log('1b', result); // should be null ?

// 2
const ref2 = await firebase.database().ref(dbref).push({ name: propValues.name, address: propValues.address });
console.log('2a', ref2.key); // should be key ?

// 3
const ref3 = await firebase.database().ref(dbref).push();
console.log('3a', ref3.key); // should be key ?
const result = await ref3.set({ name: propValues.name, address: propValues.address });
console.log('3b', result); // should be null ?

Loving react-native-firebase and the support we provide? Please consider supporting us with any of the below:

This issue has been automatically marked as stale because it has not had recent user activity. It will be closed if no further activity occurs. Thank you for your contributions.

Results with firebase 5.4.2:

1a -LLuT3yPEkWQYq7JinLP
1b undefined
2a -LLuT4A5DPPpDSjEaGg-
3a -LLuT4CbmLlncywg1l3e
3b undefined

@simonbengtsson awesome, thanks for that - looks like await support on push() is missing on RNFB - will get it fixed.

Have just pushed a re-write of the push() logic to match the Web SDK.

Have added test cases from this issue and others which can be seen working here: https://github.com/invertase/react-native-firebase/blob/database-fixes/tests/e2e/database/ref/push.js#L73

This will land in v5.1.0, apologies for the delay on this one.


Loving react-native-firebase and the support we provide? Please consider supporting us with any of the below:

Was this page helpful?
0 / 5 - 0 ratings