Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch.commit() doesn't work if it's called immediately after firebase.firestore() #2122

Closed
danbockapps opened this issue Aug 29, 2019 · 5 comments

Comments

@danbockapps
Copy link

Environment

  • Operating System version: Mac OS Mojave
  • Browser version: Chrome 76
  • Firebase SDK version: 6.4.2
  • Firebase Product: Firestore

Problem

batch.commit() is neither resolved nor rejected when run using the code below. The first message is written to the console but neither of the last two is. I made it work by moving const db = firebase.firestore() to a different place, where it would get run on app startup instead of right before the upload happens. I didn't see anything in the docs that says it's required to do that.

Relevant Code:

const onUploadClick = () => {
  const db = firebase.firestore()
  const batch = db.batch()

  const nycRef = db.collection('activities').doc('NYC')
  batch.set(nycRef, { name: 'New York City' })

  console.log('The batch is set to go.')

  batch
    .commit()
    .then(function() {
      console.log('Done.')
    })
    .catch(err => console.log(`There was an error: ${err}`))
}
@rsgowman
Copy link
Member

Your code should work fine as is. (It does for me, both as a node.js app, and as a web app.)

One thing of note though: You're not waiting on the promise returned by batch.commit(). That might not matter, but if your app exits immediately after returning from onUploadClick(), then you might not see the response. It's a race condition. And you're more likely to win the race if the Firestore initialization is started earlier which might explain why you successfully saw the console.log output in that case.

@danbockapps
Copy link
Author

It's running in the browser, so there shouldn't be any issue about the app exiting - I should be seeing "Done" in the console. Also, I should be seeing the upload in the network tab and I should be seeing data in my Firestore database, and I'm not seeing any of those things.

@rsgowman
Copy link
Member

One other possibility: Are you concurrently changing the authenticated user by any chance (incl logging out)? If we queue the batch as one user, but the authenticated user changes by the time we're ready to send the batch, then it'll be dropped. (A debug log could confirm this; see below.)

Otherwise, are you able to send over a minimal repro? (Don't forget to remove your apikey, etc before posting it though.)

Alternatively/additionally, could you enable debug logging and send the results over? (Hint: firebase.firestore.setLogLevel('debug');)

@rsgowman
Copy link
Member

rsgowman commented Sep 3, 2019

If this is auth related, then this might be the same issue as #2135 (in which case, a fix is in the works.)

Otherwise, see if you can send over a minimal repro and/or debug logs.

@danbockapps
Copy link
Author

I haven't been able to reproduce it in a minimal app, so I'm closing.

@firebase firebase locked and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants