无法使用“未定义”作为 Firestore 值。如果您想忽略未定义的值,请启用“ignoreUndefinedProperties”

问题描述 投票:0回答:2

我收到此错误

error is here: Error: Value for argument "value" is not a valid query constraint. Cannot use "undefined" as a Firestore value. If you want to ignore undefined values, enable `ignoreUndefinedProperties`. 

在我的 firebase javascript 函数中,我不知道如何解决它。我发现在打字稿中会发生此错误。但是这个错误在 javascript 中意味着什么?

这是我的职责

import * as functions from "firebase-functions";
import admin from "firebase-admin";

import {
  deleteCollection,
  deleteQuery,
  deleteUserData,
} from "../utils/deletion";
export default functions.firestore
  .document("deletions/{userUid}")
  .onDelete(async (snap, context) => {
    const db = admin.firestore();
    const { userUid } = context.params;
    const { uid } = userUid;
//dont forget to delete storage files
    try {
      await db.doc(`deletions/${userUid}/meinprofilsettings/${uid}`).delete();

        await deleteQuery(db, db.collection(`deletions/${userUid}/videos`).where("uid", "==", uid));

该行是等待删除查询...

deleteQuery 函数如下所示

async function deleteQuery(db, query, batchSize = 100) {
  const q = query.limit(batchSize);
  return new Promise((resolve, reject) => {
    deleteQueryBatch(db, q, resolve).catch(reject);
  });
}
async function deleteQueryBatch(db, query, resolve) {
  const snapshot = await query.get();
  const batchSize = snapshot.size;
  if (batchSize === 0) {
    // When there are no documents left, we are done
    resolve();
    return;
  }
  // Delete documents in a batch
  const batch = db.batch();
  snapshot.docs.forEach((doc) => {
    batch.delete(doc.ref);
  });
  await batch.commit();
  // Recurse on the next process tick, to avoid
  // exploding the stack.
  process.nextTick(() => {
    deleteQueryBatch(db, query, resolve);
  });
}

javascript google-cloud-functions
2个回答
11
投票

错误:参数“value”的值不是有效的查询约束。 无法使用“未定义”作为 Firestore 值。如果你想忽略 未定义值,启用

ignoreUndefinedProperties

这意味着您的列表中有一个“未定义”值。 Firebase 不支持列表中未定义的值,请检查您的列表并查看是否有

undefined
。您可以使用
ignoreUndefinedProperties
来避免这种情况。以下是如何实现此目标的示例:

import * as firestore from "firebase-admin";

然后只需要有这样的东西:

const db = firestore.firestore(); 
db.settings({ ignoreUndefinedProperties: true })

0
投票

我使用过滤器来实现这一点。我在 firestore 文档中没有“折扣”字段

const activeSubscriptionsSnapshot = await db.collection("subscriptions");
.where("status", "==", "ACTIVE")
.get();
const subscriptionsWithoutDiscount = activeSubscriptionsSnapshot.docs
.filter((doc) => !("discount" in doc.data()));`
© www.soinside.com 2019 - 2024. All rights reserved.