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

Firestore Transaction get() returning QuerySnapshot but it should be DocumentSnapshot #344

Closed
Rubenazo opened this issue Aug 24, 2018 · 6 comments
Assignees

Comments

@Rubenazo
Copy link

Environment

  • Operating System version: Windows 7
  • Firebase SDK version: 4.2.0
  • Library version: 6.0.0
  • Firebase Product: firestore

The problem

I'm trying to reproduce a firestore transaction with this sample code from the firebase documentation https://firebase.google.com/docs/firestore/manage-data/transactions?hl=es-419

// Initialize document
var cityRef = db.collection('cities').doc('SF');
var setCity = cityRef.set({
  name: 'San Francisco',
  state: 'CA',
  country: 'USA',
  capital: false,
  population: 860000
});

var transaction = db.runTransaction(t => {
  return t.get(cityRef)
      .then(doc => {
        // Add one person to the city population
        var newPopulation = doc.data().population + 1;
        t.update(cityRef, { population: newPopulation });
      });
}).then(result => {
  console.log('Transaction success!');
}).catch(err => {
  console.log('Transaction failure:', err);
});

However in db.runTransaction(t => { return t.get(cityRef).then(doc => { ... } ... it is say that doc must return a DocumentSnapshot, but it returns a QuerySnapshot. Even that way i can't obtain the DocumentSnapshot from doc, so there is a problem with the documentation and i'm not sure if there is also a bug with the return value.

@google-oss-bot
Copy link

Hmmm this issue does not seem to follow the issue template. Make sure you provide all the required information.

@google-oss-bot
Copy link

Hey there! I couldn't figure out what this issue is about, so I've labeled it for a human to triage. Hang tight.

@hiranya911
Copy link
Contributor

Your code compiles and works just fine for me. Here it is with some TypeScript annotations added for clarity (this compiles successfully):

    const db = admin.firestore();
    const cityRef: admin.firestore.DocumentReference = db.collection('foo').doc('bar');
    var transaction = db.runTransaction(t => {
      return t.get(cityRef)
          .then((doc: admin.firestore.DocumentSnapshot) => {
            // Add one person to the city population
            var newPopulation = doc.data().population + 1;
            t.update(cityRef, { population: newPopulation });
          });
    }).then(result => {
      console.log('Transaction success!');
    }).catch(err => {
      console.log('Transaction failure:', err);
    });

t.get() also has an overload that returns a Promise<QuerySnapshot>, but that only gets invoked if get() was called with a Query.

@hiranya911
Copy link
Contributor

One possibility is you're running the transaction on a collection reference, which extends Query.

@Rubenazo
Copy link
Author

Rubenazo commented Aug 25, 2018

@hiranya911 Thanks, i first made sure i was running the transaction in a document reference and it was indeed a document reference, so i finally made it work by explicitly declaring the types for DocumentReference and DocumentSnapshot as you did. I had my code inside a function so i did it like this:

async function setCounts(storeRef: admin.firestore.DocumentReference) { // Type here

  var transaction = admin.firestore().runTransaction(t => {
    return t.get(storeRef)
    .then((doc: admin.firestore.DocumentSnapshot) => { // Type here
      x = doc.data(); // Now i can get data() from the doc
    });
  }).then(result => {
    console.log('Transaction success!');
  }).catch(error => {
    console.log('Transaction failure:', error);
  });

}

I don't know why, but while deploying the linter inferred doc as QuerySnapshot even though it was not. There may be something to consider. Thanks again.

@schmidt-sebastian
Copy link
Contributor

Transaction.get() returns a DocumentSnapshot if you pass a DocumentReference (https://github.com/googleapis/nodejs-firestore/blob/master/dev/src/transaction.js#L89) and a QuerySnapshot if you pass a Query or a CollectionReference (https://github.com/googleapis/nodejs-firestore/blob/master/dev/src/transaction.js#L97). Everything else should get rejected.

Please let us know if you still encounter this issue. I am closing this as it looks like it is working for you now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants