![](/img/trans.png)
[英]Cloud function to export Firestore backup data. Using firebase-admin or @google-cloud/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 之外的所有模擬器:
firebase serve --only hosting
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
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.