英文:
Duplicate Firestore subcollection created upon updating existing records with Google App Script Extension
问题
我使用Google App Script扩展来通过这个精心编写的库(https://github.com/grahamearley/FirestoreGoogleAppsScript)从Google表格中导入我的现有数据/行。
我尝试使用firestore.updateDocument函数来更新Firestore中与一组主键标识的现有文档,这些文档与googlesheets中的现有行进行比较后更新。
基于JavaScript的代码似乎能够在运行更新函数之前识别现有文档,但实际上它会在目标集合中创建一个名为"subcollection project"的子集合。这导致我的应用程序无法检索到最新的更新记录,而只能获取原始过时的Firestore文档。
请参考截图。
我按照库的说明进行了操作,有谁擅长JavaScript的人可以查看我的代码并指导我吗?
var collectionName = "collectionNameTest"
// 检查是否在现有的Firestore集合中存在具有相同主键的文档
var duplicateDocs = firestore.query(collectionName)
.Where(primaryKey1Column,"==",primaryKey1)
.Where(primaryKey2Column,"==",primaryKey2)
.Execute();
if (duplicateDocs.length > 0) {
// 具有相同主键的文档已存在于集合中
const name = duplicateDocs[0].name
console.log(`Documents with primaryKey1 '${primaryKey1}' and with primaryKey2 '${primaryKey2}' already exist. Updating existing record with new record...`);
try {
var result = firestore.updateDocument(`${collectionName}/${name}`, data);
console.log("Firestore document updated, Firestore result: ", result);
} catch (e) {
console.error("Firestore update failed: ", e);
}
} else {
// 不存在具有相同主键的文档,因此创建新文档
var result = firestore.createDocument(collectionName, data);
console.log("New Firestore document created, Firestore result: ", result);
}
英文:
I am using Google App Script extension to import my existing Data/rows from Google sheet by using this well-written library - https://github.com/grahamearley/FirestoreGoogleAppsScript
I tried to use firestore.updateDocument function to update existing documents within the Firestore that are identified by a set of primary keys, after comparing them to the existing rows in googlesheets.
The code, which is based on Javascript, I believe, works to identify existing documents before the update function runs, but it actually - results in creating a "subcollection project" in the target collection instead. This results in an issue where my app is not able to retrieve latest updated records but only the original outdated Firestore documents.
Kindly refer to the screenshot.
I followed library instructions and everything, could someone who is good at javascript look into my code and guide me?
var collectionName = "collectionNameTest"
// Check if a document with the same primary keys exists in the existing Firestore collection
var duplicateDocs = firestore.query(collectionName)
.Where(primaryKey1Column,"==",primaryKey1)
.Where(primaryKey2Column,"==",primaryKey2)
.Execute();
if (duplicateDocs.length > 0) {
// A document with the same primary key already exists in the collection
const name = duplicateDocs[0].name
console.log(`Documents with primaryKey1 '${primaryKey1}' and with primaryKey2 '${primaryKey2}' already exists. Updating existing record with new record...`);
try { var result = firestore.updateDocument(`${collectionName}/${name}`, data);
console.log("Firestore document updated, Firestore result: ", result);
} catch (e) {
console.error("Firestore update failed: ", e);
}
} else {
// No document with the same primary key exists, so create a new document
var result = firestore.createDocument(collectionName, data);
console.log("New Firestore document created, Firestore result: ", result);
}
答案1
得分: 1
从:
const name = duplicateDocs[0].name
到:
const name = duplicateDocs[0].name.split("/").pop();
注:
- 在这种情况下,假设
data
是有效值。请注意这一点。
参考:
英文:
In your situation, how about the following modification?
From:
const name = duplicateDocs[0].name
To:
const name = duplicateDocs[0].name.split("/").pop();
Note:
- In this case, it supposes that
data
is a valid value. Please be careful about this.
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论