我将以下代码与 Firebase SDK 8 一起使用。
const db = firebase.firestore();
const collectionRef = db.collection(collectionName);
var query = collectionRef.where('isFinal', '==', true);
query = query.where(firebase.firestore.FieldPath.documentId(), 'in', docIds);
return query;
我想用模块化 SDK 替换它的代码。所以我写了,
const dbInstance = getFirestore(firebaseApp);
const collectionRef = collection(dbInstance, collectionName);
query(collectionRef, where(???, 'in', docIds));
return query;
但不知何故,我找不到获取 FieldPath 的语法。从参考文献中,我可以读到,
/**
* Returns a special sentinel `FieldPath` to refer to the ID of a document.
* It can be used in queries to sort or filter by the document ID.
*/
export declare function documentId(): FieldPath;
这是进口的
import {documentId} from 'firebase/firestore';
但是当我使用它时,它会导致错误。
有谁知道正确的语法是什么?
Thanks
EDIT - 1这是我用来从 firestore 获取文档的代码
//docIds is an array
export const getRecordsByIds = (docIds) => {
const promise = new Promise(async (resolve, reject) => {
try {
let experiences = await getByIds(docIds);
resolve(experiences);
} catch (error) {
console.log(error);
reject(error);
}
});
return promise;
};
//Service
export const getByIds = (docIds) => {
return new Promise(async (resolve, reject) => {
try {
const documents = await getDocumentWithQuery(
getByIdQuery(docIds, FirebaseCollection.Experiences)
);
if (documents.docs.length > 0) {
const experiences = await parseExperience(documents.docs);
resolve(experiences);
} else {
reject(docNotExistError);
}
} catch (error) {
reject(error);
}
});
};
//Query generator
export const getByIdQuery = (docIds, collectionName) => {
const collectionRef = collection(dbInstance, collectionName);
console.log(docIds);
query(collectionRef, where(documentId(), "in", docIds));
return query;
};
//FirebaseFirestore.web.js
export const getDocumentWithQuery = (query) => {
const promise = new Promise(async (resolve, reject) => {
try {
const documents = await getDocs(query);
if (documents) {
resolve(documents);
} else {
resolve({});
}
} catch (e) {
console.error('Error retrieving documents: ', e);
reject(e);
}
});
return promise;
};
getRecordsByIds
是入口点。