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

.where(admin.firestore.FieldPath.documentId(), 'in', ['id1', 'id2']) fails: '... FieldPath.documentId() must be a string or a DocumentReference' #990

Closed
xaphod opened this issue Mar 28, 2020 · 11 comments · Fixed by #993
Assignees
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. triage me I really want to be triaged.

Comments

@xaphod
Copy link

xaphod commented Mar 28, 2020

Environment details

  • OS: Firebase Function NodeJS, running on the emulator from Firebase CLI 7.16.1
  • Node.js version: 13.11.0
  • @google-cloud/firestore version: 3.7.2
  • firebase-admin version: 8.10.0

Steps to reproduce

    const admin = require('firebase-admin');
    const arr = ['id1', 'id2'];
    const m = await db
      .collection('media')
      .where(admin.firestore.FieldPath.documentId(), 'in', arr)
      .get();

Expected: able to query a list of document IDs in a single collection.

Actual:

Error: The corresponding value for FieldPath.documentId() must be a string or a DocumentReference.
    at CollectionReference.validateReference (node_modules/@google-cloud/firestore/build/src/reference.js:1325:19)
    at CollectionReference.where (node_modules/@google-cloud/firestore/build/src/reference.js:1038:26)

Does anyone know a workaround? Thanks! (edited several times for clarity)

@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/nodejs-firestore API. label Mar 28, 2020
@xaphod xaphod changed the title .where(admin.firestore.FieldPath.documentId(), 'in', ['id1', 'id2']) fails with 'The corresponding value for FieldPath.documentId() must be a string or a DocumentReference' .where(admin.firestore.FieldPath.documentId(), 'in', ['id1', 'id2']) fails: '... FieldPath.documentId() must be a string or a DocumentReference' Mar 28, 2020
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Mar 28, 2020
@Padreco-lbr
Copy link

Padreco-lbr commented Mar 30, 2020

Happening to me too, if not an issue, please provide information ou how to make a query based on a list of document Ids. There simply is no documentation examples on that.

My current workaround is to use firestore's getAll:

refs = arr.map(doc => db.collection('media').doc(doc));
const m = await db.getAll(...refs);

@schmidt-sebastian schmidt-sebastian self-assigned this Mar 31, 2020
@schmidt-sebastian
Copy link
Contributor

I will get this fixed today.

@xaphod
Copy link
Author

xaphod commented Mar 31, 2020

Thanks!

@pennjlin
Copy link

Looks like this issue persists when running with Firebase local emulator. Is there a way to verify the package version used in the emulator? @schmidt-sebastian

@schmidt-sebastian
Copy link
Contributor

You can always use npm list to verify the installed version. In general, removing the package.lock and node_modules followed by a re-install should get you the latest version.

@ebeloded
Copy link

My firebase-admin is up to date, yet I am still running into this exact issue. Any suggestions?

@ebeloded
Copy link

After looking at the tests, I tried to use @google-cloud/firestore instead of firebase-admin, and it worked!

This works:

import { FieldPath, Firestore } from '@google-cloud/firestore'

const firestore = new Firestore()

const menus = await firestore
  .collection('menus')
  .where(FieldPath.documentId(), 'in', ['cUKi4Kj42LOcLsqBZOBhM'])
  .get()

This doesn't:

import admin from 'firebase-admin'

const menus = await admin
  .firestore()
  .collection('menus')
  .where(admin.firestore.FieldPath.documentId(), 'in', [
    'cUKi4Kj42LOcLsqBZOBhM',
  ])
  .get()

}

start()

It seems that admin.firestore.FieldPath is not the same as FieldPath from @google-cloud/firestore

@MathiasGilson
Copy link

This works for me

const firebase = require("@firebase/testing")
const admin = firebase.initializeAdminApp({
    projectId: "my-test-project"
})

// ... //

// it as to be firebase from @firebase/testing
admin.firestore()
  .collection("users")
  .where(firebase.firestore.FieldPath.documentId(), "in", userIds) 
  .get()

@pablomaribondo
Copy link

pablomaribondo commented Nov 28, 2020

This works for me

const admin = require("firebase-admin");

const arr = ["id1", "id2"];
const refArr = arr.map(id => admin.firestore().collection("media").doc(id));

const m = await admin
      .firestore()
      .collection("media")
      .where(admin.firestore.FieldPath.documentId(), "in", refArr)
      .get();

@5amfung
Copy link

5amfung commented Dec 26, 2020

Somehow the query works without orderBy. As soon as I added orderBy, I get empty result set.

const query = admin.firestore()
  .collection('users')
  .where(firebase.firestore.FieldPath.documentId(), 'in', userIds) 
  .orderBy('firstName');
const results = await transaction.get(query);

@schmidt-sebastian
Copy link
Contributor

@5amfung The query you are issuing should be rejected by the backend ("inequality filter property and first sort order must be the same: key and firstName"). If you are using an "in" filter, you also need to use this field as the first orderBy constraint. You can then add additional orderBy constraints afterwards (e.g. where(FieldPath.documentId(), 'in', [...]).orderBy(FieldPath.documentId()).orderBy('firstName')).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. triage me I really want to be triaged.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants