I am using the below code with the Firebase SDK 8.
JavaScript
x
8
1
const db = firebase.firestore();
2
const collectionRef = db.collection(collectionName);
3
4
var query = collectionRef.where('isFinal', '==', true);
5
6
query = query.where(firebase.firestore.FieldPath.documentId(), 'in', docIds);
7
return query;
8
I want to replace it code with the modular SDK. So I have written,
JavaScript
1
6
1
const dbInstance = getFirestore(firebaseApp);
2
const collectionRef = collection(dbInstance, collectionName);
3
query(collectionRef, where(???, 'in', docIds));
4
5
return query;
6
But somehow I could not find the syntax to get the FieldPath. From the reference, I can read that,
JavaScript
1
6
1
/**
2
* Returns a special sentinel `FieldPath` to refer to the ID of a document.
3
* It can be used in queries to sort or filter by the document ID.
4
*/
5
export declare function documentId(): FieldPath;
6
which is imported with
JavaScript
1
2
1
import {documentId} from 'firebase/firestore';
2
But when I am using it, it results in an error.
Does anyone know what is the correct syntax for this?
Thanks
EDIT – 1 Here is the code I am using to get the documents from firestore
JavaScript
1
61
61
1
//docIds is an array
2
export const getRecordsByIds = (docIds) => {
3
const promise = new Promise(async (resolve, reject) => {
4
try {
5
let experiences = await getByIds(docIds);
6
resolve(experiences);
7
} catch (error) {
8
console.log(error);
9
reject(error);
10
}
11
});
12
return promise;
13
};
14
15
//Service
16
export const getByIds = (docIds) => {
17
return new Promise(async (resolve, reject) => {
18
try {
19
const documents = await getDocumentWithQuery(
20
getByIdQuery(docIds, FirebaseCollection.Experiences)
21
);
22
if (documents.docs.length > 0) {
23
const experiences = await parseExperience(documents.docs);
24
resolve(experiences);
25
} else {
26
reject(docNotExistError);
27
}
28
} catch (error) {
29
reject(error);
30
}
31
});
32
};
33
34
//Query generator
35
export const getByIdQuery = (docIds, collectionName) => {
36
const collectionRef = collection(dbInstance, collectionName);
37
console.log(docIds);
38
query(collectionRef, where(documentId(), "in", docIds));
39
return query;
40
};
41
42
43
44
//FirebaseFirestore.web.js
45
export const getDocumentWithQuery = (query) => {
46
const promise = new Promise(async (resolve, reject) => {
47
try {
48
const documents = await getDocs(query);
49
if (documents) {
50
resolve(documents);
51
} else {
52
resolve({});
53
}
54
} catch (e) {
55
console.error('Error retrieving documents: ', e);
56
reject(e);
57
}
58
});
59
return promise;
60
};
61
getRecordsByIds
is the entry point.
Advertisement
Answer
Your syntax looks correct and is working. Just replace the ???
with documentId()
. Did you forget the ()
by chance?
JavaScript
1
20
20
1
import { initializeApp } from "firebase/app";
2
import {
3
collection,
4
getFirestore,
5
query,
6
where,
7
documentId,
8
getDocs,
9
} from "firebase/firestore";
10
11
const firebaseConfig = { };
12
initializeApp(firebaseConfig);
13
14
const dbInstance = getFirestore();
15
const collectionRef = collection(dbInstance, "test");
16
17
const q = query(collectionRef, where(documentId(), "in", ["test"]));
18
const querySnap = await getDocs(q);
19
console.log(querySnap.size);
20
Firebase JS SDK version: 9.0.0-beta.6
The problem seems to be here:
JavaScript
1
10
10
1
export const getByIdQuery = (docIds, collectionName) => {
2
const collectionRef = collection(dbInstance, collectionName);
3
console.log(docIds);
4
query(collectionRef, where(documentId(), "in", docIds));
5
6
return query;
7
// ^^^ You are returning the "query" function
8
// imported from "firebase/firestore"
9
};
10
Assign the query to a variable and then return it:
JavaScript
1
7
1
export const getByIdQuery = (docIds, collectionName) => {
2
const collectionRef = collection(dbInstance, collectionName);
3
console.log(docIds);
4
const newQuery = query(collectionRef, where(documentId(), "in", docIds));
5
return newQuery;
6
};
7