【问题标题】:Firestore (4.10.1): Could not reach Firestore backend. Firestore Access Issues In Cloud FunctionsFirestore (4.10.1):无法访问 Firestore 后端。 Cloud Functions 中的 Firestore 访问问题
【发布时间】:2018-09-15 20:18:30
【问题描述】:

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

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);
              });
            }
          });
     });
 });

此时我被卡住了。据我所知,我已经做好了准备,但也许没有?我搜索了文档并用谷歌搜索了这个问题,但没有取得多大成功。你看出什么不对了吗?

【问题讨论】:

    标签: firebase vue.js google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您是否安装了 Firebase CLI?

     npm install -g firebase-tools
    

    您是否通过 CLI 登录到 Firebase 控制台?

    firebase login
    

    您是否初始化了 Firebase Cloud Functions ?

    firebase init functions
    

    然后你不需要重新初始化应用程序,初始化一个管理应用程序实例,想。 这是一个示例,希望对您有所帮助

    const functions = require('firebase-functions')
    const admin = require('firebase-admin')
    admin.initializeApp(functions.config().firebase)
    //trigger a function to fire when new user document created
    exports.createUser = functions.firestore
        .document('users/{userId}')
        .onCreate(event => {
            // perform desired operations ...
        });
    

    部署你的功能

    firebase deploy --only functions
    

    在此处阅读更多信息https://firebase.google.com/docs/functions/get-started

    【讨论】:

    • 感谢您的帮助!它为我指明了正确的方向。我会指出,我使用的是 Web 实现而不是节点,因此您在上面放置的实现不适用于我的实例。但是,我从未指定过,这是我的错。
    【解决方案2】:

    好的。所以我的问题的答案是我不是很聪明。非常感谢 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 后端。

    并没有很好地为我指明那个方向。

    【讨论】:

      猜你喜欢
      • 2020-02-01
      • 2018-09-10
      • 2019-02-07
      • 2020-09-29
      • 2018-06-29
      • 2020-01-06
      • 2021-08-23
      • 1970-01-01
      • 2018-11-03
      相关资源
      最近更新 更多