簡體   English   中英

使用在 Firestore 模擬器上運行的 Firebase-Admin 時找不到 Firestore 文檔

[英]Firestore document not found when using Firebase-Admin running on Firestore Emulator

當我在客戶端使用firebase.firestore()時,以下 function 運行良好並設法找到了文檔:

firebase.firestore()

function getUserProfile(email, returnDoc) {
    var db = firebase.firestore();
    var docRef = db.collection("Users").doc(email);

    docRef.get().then(function (doc) {
        if (doc.exists) {
            returnDoc(undefined, doc);
        } else {
            returnDoc("User not found.", doc);
        }
    }).catch(function (error) {
        returnDoc("Error getting user.", doc);
    });
};

index.js

getUserProfile(user.email, function (err, userProfile) {
   if (!err) {
      $scope.firstName = userProfile.get("FirstName");
      $scope.lastName = userProfile.get("LastName");
      $scope.email = userProfile.get("Email");
      $scope.$apply();
   } else {
      alert(err);
    };
});

但是當我嘗試使用 firebase firebase-admin創建另一個類似的 function 時,以下 function找不到具有相同email參數的文檔:

db.js使用admin.firestore()

const admin = require('firebase-admin');  
admin.initializeApp();

let db = admin.firestore();

function getUserData(email, returnDoc) {
    console.log(`imtp-db.getUserData: ${email}`); // email data is correct and exist in database.

    let docRef = db.collection("Users").doc(email);

    return docRef.get().then(function (doc) {
        console.log(`doc.exists: ${doc.exists}`); // doc.exists: false here.
        if (doc.exists) {
            console.log("Document data:", doc.data());

            return returnDoc(undefined, doc);
        } else {
            return returnDoc("User not found.", doc);
        };
    }).catch(function (error) {
        return returnDoc("Error getting user.", doc);
    });
};

exports.getUserData = getUserData;

在雲端 Function:

const functions = require('firebase-functions');
const db = require("./middleware/db.js");

exports.getUserProfile = functions.https.onCall((data, context) => {
    var userProfile = undefined;
    console.log(`data.email = ${data.email}`);
    return db.getUserData(data.email, function (err, userDoc) {
        console.log(`exports.getUserProfile.err = ${err}`);
        if (!err) {
            userProfile = userDoc.data();
            return {
                error: err,
                returnData: userProfile
            };
        } else {
            return {
                error: err,
                returnData: userProfile
            };
        };
    });
});

在上面的 function 中一切正常,沒有錯誤,除了doc.exists始終評估為false並始終返回"User not found." , 為什么? 我在第二版代碼中做錯了什么? 謝謝!

注意:我正在運行所有模擬器: firebase emulators:start

顯然,參考這篇文章,答案很簡單:本地 Firestore 模擬器沒有數據 這可以通過基於問題中完全相同的代碼的以下方法輕松證明:

啟動除 Firestore Emulator 之外的所有模擬器:

  1. firebase serve --only hosting
  2. firebase emulators:start --only functions

現在文檔可以訪問並且doc.exists == true

在此處輸入圖像描述

如果不啟動Firestore 模擬器,代碼將被迫使用數據訪問生產服務器。 當然,不建議這樣做,尤其是當您使用Blaze Plan時。 因此,如上述鏈接所示,我們應該在使用 Firestore Emulator 之前在本地設置測試數據。

至於客戶端firebase.firestore()能夠訪問數據的原因,這是因為 Firestore Emulator 運行在與主機不同的端口上。 因此,在web 客戶端index.js中執行的firebase.firestore()正在使用localhost:5000 ,繞過 Firestore 模擬器並直接進入生產服務器。 要解決此問題,此Firebase 文檔可能是解決方案(但尚未測試):

// Initialize your Web app as described in the Get started for Web
// firebaseApp previously initialized using firebase.initializeApp().
var db = firebaseApp.firestore();
if (location.hostname === "localhost") {
  db.settings({
    host: "localhost:8080",
    ssl: false
  });
}

當您使用模擬器時,Firebase 不會從實際的 Firestore 數據庫中獲取。 它從 Firestore 模擬器中獲取,您可以使用 firebase 模擬器 ui 訪問該模擬器,以管理那里的數據。 Firestore 模擬器與您的實際 Firestore 數據庫不同。

如果您運行 firebase 仿真器:開始您可以查看 url 以訪問仿真器 ui 在此處輸入圖像描述

暫無
暫無

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

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