0

I'm developing a react-native app with react-native-firebase, and I'm trying to implement a transaction. here's my code:

const roomRef = firebase.database().ref('rooms').child(roomKey)
return roomRef.transaction(function(room) {
  console.log('TRANSACTION STARTED')
  if (room && !room.joiner) {
    room.joiner = userId
  }
  return room
}, function(error, committed, snap) {
  if (error) console.log(error)
  if (committed) console.log('COMMITTED')
  console.log(snap.val())
})

The problem is that this transaction function seems to be not executed. I'm sure that previous code run, but no updates in database appear, no console.log, nothing. Anybody can help me please?

EDIT

I noticed that no transaction works in my app, whatever the conditions are. In cloud functions I'm using the admin SDK, and in this case transactions work.

Dario Ielardi
  • 807
  • 9
  • 24
  • There's nothing wrong with the code you've posted. If you've used `firebase.initializeApp()` correctly, you should at least get the console.log output (even if you have permission issues or your ref tree is incorrect). You may need to post your initialization as well. – Adam Kipnis Jan 18 '18 at 16:25
  • Thanks for your answer. The reference is correct, and the database rules simply allow every action on db. With the react-native-firebase library I don't need to initializeApp() cause I've placed google-services.json in android project. I noticed that no transaction works in my app, whatever the conditions are. In cloud functions I'm using the admin SDK, and in this case transactions work. – Dario Ielardi Jan 18 '18 at 17:07
  • You can initialize your DB both ways. My guess is your google-services.json isn't being picked up correctly (I had problems with this approach as well). A simple test would be to use `initializeApp()` and see if that solves the issue. If so, then you've narrowed down your problem. – Adam Kipnis Jan 18 '18 at 17:16
  • If I try to call initializeApp() the debugger gives me a warning 'calling initializeApp() for apps that are already initialised natively is unnecessary.' Anyway I got several operations with firebase in my app, like auth operations and firestore/realtime db writes and reads. Everything works except transactions. – Dario Ielardi Jan 18 '18 at 17:25
  • Did you ever resolve this issue? I'm having the same problem – Piotr Jun 07 '18 at 18:40
  • I figured out that when this problem came up there was no transactions implementation in react-native-firebase, but now they did it. So maybe you just need to get the last version. – Dario Ielardi Jun 08 '18 at 12:16

0 Answers0