Firebase 9(模块化 sdk web )替换 fieldPath

Har*_*han 6 javascript web firebase google-cloud-firestore

我将以下代码与 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;
Run Code Online (Sandbox Code Playgroud)

我想用模块化 SDK 替换它的代码。所以我写了,

const dbInstance = getFirestore(firebaseApp);
const collectionRef = collection(dbInstance, collectionName);
query(collectionRef, where(???, 'in', docIds));

return query;
Run Code Online (Sandbox Code Playgroud)

但不知何故,我找不到获取 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;
Run Code Online (Sandbox Code Playgroud)

这是进口的

import {documentId} from 'firebase/firestore';
Run Code Online (Sandbox Code Playgroud)

但是当我使用它时,它会导致错误。

有谁知道正确的语法是什么?

谢谢

编辑 - 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;
};
Run Code Online (Sandbox Code Playgroud)

getRecordsByIds是入口点。

Dha*_*raj 4

您的语法看起来正确并且有效。只需将 替换???documentId(). 你是否()偶然忘记了?

import { initializeApp } from "firebase/app";
import {
  collection,
  getFirestore,
  query,
  where,
  documentId,
  getDocs,
} from "firebase/firestore";

const firebaseConfig = {...};
initializeApp(firebaseConfig);

const dbInstance = getFirestore();
const collectionRef = collection(dbInstance, "test");

const q = query(collectionRef, where(documentId(), "in", ["test"]));
const querySnap = await getDocs(q);
console.log(querySnap.size); 
Run Code Online (Sandbox Code Playgroud)

Firebase JS SDK 版本:9.0.0-beta.6

问题似乎就在这里:

export const getByIdQuery = (docIds, collectionName) => {
  const collectionRef = collection(dbInstance, collectionName);
  console.log(docIds);
  query(collectionRef, where(documentId(), "in", docIds));
  
  return query;
  // ^^^ You are returning the "query" function
  // imported from "firebase/firestore"
};
Run Code Online (Sandbox Code Playgroud)

将查询分配给变量然后返回它:

export const getByIdQuery = (docIds, collectionName) => {
  const collectionRef = collection(dbInstance, collectionName);
  console.log(docIds);
  const newQuery = query(collectionRef, where(documentId(), "in", docIds));
  return newQuery;
};
Run Code Online (Sandbox Code Playgroud)