簡體   English   中英

AsyncQueue 無法持久寫入:TypeError:無法讀取未定義的屬性(讀取“toString”)

[英]AsyncQueue Failed to persist write: TypeError: Cannot read properties of undefined (reading 'toString')

我正在嘗試為我的 Firestore 規則編寫單元測試。 它松散地基於https://github.com/firebase/quickstart-testing/blob/master/unit-test-security-rules/test/firestore.spec.js

並遵循https://firebase.google.com/docs/firestore/security/test-rules-emulator

它有 3 個測試用例,其中 2 個通過。 第三個測試是用戶無法刪除文檔,但deleteDoc似乎會引發錯誤。

[2022-10-11T19:56:27.376Z]  @firebase/firestore: Firestore (9.11.0): AsyncQueue Failed to persist write: TypeError: Cannot read properties of undefined (reading 'toString')
[2022-10-11T19:56:27.377Z]  @firebase/firestore: Firestore (9.11.0): INTERNAL UNHANDLED ERROR:  TypeError: Cannot read properties of undefined (reading 'toString')
    at ObjectMap.mapKeyFn (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/model/collections.ts:86:16)
    at ObjectMap.get (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/util/obj_map.ts:49:21)
    at ObjectMap.has (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/util/obj_map.ts:63:17)
    at /Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/local_documents_view.ts:190:21
    at /Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/util/sorted_map.ts:141:7
    at LLRBNode.inorderTraversal (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/util/sorted_map.ts:324:7)
    at SortedMap.inorderTraversal (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/util/sorted_map.ts:136:42)
    at SortedMap.forEach (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/util/sorted_map.ts:140:10)
    at LocalDocumentsView.populateOverlays (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/local_documents_view.ts:189:10)
    at LocalDocumentsView.getOverlayedDocuments (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/local_documents_view.ts:174:17)
    at /Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/local_store_impl.ts:350:48
    at /Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/persistence_promise.ts:136:42
    at PersistencePromise.wrapUserFunction (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/persistence_promise.ts:120:22)
    at PersistencePromise.wrapSuccess (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/persistence_promise.ts:136:19)
    at PersistencePromise.next (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/persistence_promise.ts:94:21)
    at /Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/local_store_impl.ts:346:10
    at MemoryPersistence.runTransaction (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/memory_persistence.ts:175:12)
    at localStoreWriteLocally (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/local/local_store_impl.ts:327:6)
    at syncEngineWrite (/Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/core/sync_engine_impl.ts:462:26)
    at /Users/gustek/my-github/db-unit-tests-example/functions/node_modules/@firebase/firestore/src/core/firestore_client.ts:455:12
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

這是我的測試代碼

import {assertFails, assertSucceeds} from "@firebase/rules-unit-testing";
import firebase = require("@firebase/rules-unit-testing");
import fs = require("fs");
import {setDoc, deleteDoc} from "firebase/firestore";

const PROJECT_ID = "firestore-emulator-example";


describe("Rules test", function() {
  let env: firebase.RulesTestEnvironment;

  before(async () => {
  // Load the rules file before the tests begin
    const rules = fs.readFileSync("../firestore.rules", "utf8");
    env = await firebase.initializeTestEnvironment(
        {
          projectId: PROJECT_ID,
          firestore: {rules},
        }
    );
  });

  beforeEach(async () => {
  // Clear the database between tests
    await env.clearFirestore();
  });

  it("require users to log in before creating a profile", async () => {
    const userId = "alice";
    const context = env.authenticatedContext(userId);
    await assertSucceeds(
        setDoc(
            context.firestore().doc(`/privateProfiles/${userId}`),
            {}
        )
    );
  });

  it("edit only your own profile", async () => {
    const alice = "alice";
    const bob = "bob";
    const aliceContext = env.authenticatedContext(alice);
    const bobContext = env.authenticatedContext(bob);
    await assertSucceeds(
        setDoc(
            aliceContext.firestore().doc(`/privateProfiles/${alice}`),
            {}
        )
    );
    await assertFails(
        setDoc(
            bobContext.firestore().doc(`/privateProfiles/${alice}`),
            {}
        )
    );
  });

  it("can't directly delete a profile", async () => {
    const alice = "alice";
    const aliceContext = env.authenticatedContext(alice);
    const db = aliceContext.firestore();
    await assertSucceeds(
        setDoc(
            db.doc(`/privateProfiles/${alice}`),
            {"smth": 1}
        )
    );
    await assertFails(
        deleteDoc(
            db.doc(`/privateProfiles/${alice}`)
        )
    );
  });
});

完整代碼位於https://github.com/GustekDev/db-unit-tests-example

使用firebase emulators:exec./test.sh從內部functions目錄運行測試。

我做錯了什么,還是 Firebase 中的錯誤?

該問題已通過更改解決

assertSucceeds(setDoc(alice.firestore(), '/users/alice'), { ... })

assertSucceeds(setDoc(doc(alice.firestore(), '/users/alice'), { ... }))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM