gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:38:15 26 4
gpt4 key购买 nike

我将以下代码与 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';
但是当我使用它时,它会导致错误。
有谁知道这个的正确语法是什么?
谢谢
编辑 - 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是入口点。

最佳答案

您的语法看起来正确并且正在工作。只需更换 ???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;
};

关于javascript - Firebase 9(模块化 sdk web)替换 fieldPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68441774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com