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

2024-04-19

我将以下代码与 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是入口点。


您的语法看起来正确并且有效。只需更换??? with 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); 

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"
};

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

export const getByIdQuery = (docIds, collectionName) => {
  const collectionRef = collection(dbInstance, collectionName);
  console.log(docIds);
  const newQuery = query(collectionRef, where(documentId(), "in", docIds));
  return newQuery;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Firebase 9(模块化 sdk web )替换 fieldPath 的相关文章