gpt4 book ai didi

firebase - Firestore (4.10.1) : Could not reach Firestore backend. Cloud Functions 中的 Firestore 访问问题

转载 作者:搜寻专家 更新时间:2023-10-30 22:33:39 27 4
gpt4 key购买 nike

快速提问。长话短说,我在我的谷歌云功能日志中收到此错误:

Firestore (4.10.1):无法到达 Firestore 后端。

这是我的函数文件中的代码:

 // pull in firebase
const firebase = require('firebase');
// required
require("firebase/firestore");
// initialize firebase
const firebaseApp = firebase.initializeApp({
// Firebase configuration.
apiKey: "<Key Here>",
authDomain: "<Auth Domain>",
databaseURL: "<database url>",
projectId: "<project id>",
storageBucket: "<storage bucket>",
messagingSenderId: "<messaging sender id>"
});

// setup the firestore
var fs = firebaseApp.firestore();

exports.search = functions.https.onRequest((request, response) => {
cors(request, response, () => {

// set a reference to the foo table in firestore
var docRef = fs.collection("foo");
// check for the foo in the firestore
docRef.where('bar', '==', <something>).get().then(function(doc) {
if (!doc.docs) {
return db.collection("foo").add({
bar: <something>
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
}
});
});
});

此时我被卡住了。据我所知,我已经设置好了,但也许没有?我已经搜索了文档并用谷歌搜索了这个问题,但没有太大的成功。你看有什么不对吗?

最佳答案

好的。所以我的问题的答案是我不是很聪明。非常感谢 Taha Azzabi 为我指明了正确的方向。原来我的问题出在这里:

docRef.where('bar', '==', <something>).get().then(function(doc) {
if (!doc.docs) {
return db.collection("foo").add({
bar: <something>
})

这永远行不通。我的查询是正确的,但对 doc.docs 的检查不正确。我的代码现在是:

// setup the firestore
const fs = firebase.firestore();
// set a reference to the document for the searched summoner
var docRef = fs.collection("bars").doc("snickers");
// check for the bar in the firestore
return docRef.get()
.then(function(doc) {
if (!doc.docs) {
return fs.collection("bars").doc("snickers").set({
name: "snickers"
})
.then(function(reference) {
console.log("Document written");
return response.status(200).send('');
})

这就是我一直在寻找的东西,所以我很高兴。长话短说,我正在抓取一组结果,然后试图检查是否存在单个结果。我需要做的是从 firestore 中获取一个文档,然后从那里检查该文档是否存在。但是,错误:

Firestore (4.10.1):无法到达 Firestore 后端。

在向我指出那个方向方面并没有真正做得很好。

关于firebase - Firestore (4.10.1) : Could not reach Firestore backend. Cloud Functions 中的 Firestore 访问问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49682020/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com