【问题标题】:Google Firebase Web App - Error involving Cloud Functions and FirestoreGoogle Firebase Web App - 涉及 Cloud Functions 和 Firestore 的错误
【发布时间】:2018-09-25 17:17:48
【问题描述】:

最近,我尝试关注一些在线演示,以开始使用 Google Firebase Cloud Functions Node.js 应用程序的服务器端代码。在尝试添加 Firebase 应用(和 Firestore)时,

const functions = require('firebase-functions');
const firebase = require('firebase-admin');
require('firebase/firestore');

var serviceAccount = require("<private key>.json");

const initAppFirebase = firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccount),
    databaseURL: "<URL>"
});

var db = firebase.firestore();

我开始收到一个错误,其中提到了 google-gax 模块中缺少“annotations.proto”文件(这显然是在 Firestore 中的某个时间点导入的)。

Error during trigger parsing:  Error occurred while parsing your function 
triggers.
Error: The include `google\api\annotations.proto` was not found.
    at Function.GoogleProtoFilesRoot._findIncludePath 
(C:\...\functions\node_modules\google-gax\lib\grpc.js:312:11)
    at GoogleProtoFilesRoot.resolvePath 
(C:\...\functions\node_modules\google-gax\lib\grpc.js:298:31)
...
    at v1beta1 (C:\...\functions\node_modules\@google- 
cloud\firestore\src\v1beta1\index.js:30:10)
    at new Firestore (C:\...\functions\node_modules\@google- 
cloud\firestore\src\index.js:229:18)

我在互联网上四处寻找,但似乎没有其他人有这个问题。我刚刚重新安装了文件,但我仍然遇到这个问题。删除 Firestore 和 Firebase initializeApp 代码允许它工作,所以我相信它与此有关。

这是我来自package.json的模块版本

"firebase-admin": "^5.12.0",
"firebase-functions": "^1.0.1",
"firestore": "^1.1.6",

有没有办法解决这个问题(安装拙劣、库过时、缺少代码/要求等)?非常感谢。

编辑:为版本信息添加了代码和 package.json。我在实际文件\node_modules\google-gax\lib\grpc.js 中进行了挖掘,发现它试图通过在每个父目录中测试google\api\annotations.proto 来返回通向导入文件annotation.proto 的有效路径。

var current = originPath;
var found = fs.existsSync(path.join(current, includePath));
while (!found && current.length > 0) {
  current = current.substring(0, current.lastIndexOf(path.sep));
  found = fs.existsSync(path.join(current, includePath));
}
if (!found) {
  throw new Error('The include `' + includePath + '` was not found.');
}
return path.join(current, includePath);

不幸的是,没有这样的目录,尽管它可能正在寻找google-proto-files\google\api\annotations.proto 中的注释文件(但手动添加该文件会导致进一步的错误)。这里还有一个github问题https://github.com/googleapis/nodejs-firestore/issues/175提到它。

【问题讨论】:

  • 您能否编辑您的问题以显示出现问题的完整、最少的代码(不仅仅是几行)?
  • 抱歉不清楚!我会尽快添加我的代码。
  • 重要的是你没有显示firebase的定义。

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


【解决方案1】:

您说您正在尝试构建一个网络应用程序。但是您使用的代码是用于在(服务器端)Node.js 脚本中初始化 Firebase Admin SDK。

如果您想在您的网络应用程序中使用 Firestore,请从您单击 this page 上的 WEB 选项卡时看到的代码开始:

<script src="https://www.gstatic.com/firebasejs/4.12.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.12.0/firebase-firestore.js"></script>

然后:

firebase.initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});

// Initialize Cloud Firestore through Firebase
var db = firebase.firestore();

您可以通过以下方式获取 initializeApp 调用中的值:

  1. 转到Project overview page
  2. 点击添加另一个应用程序
  3. 点击将 Firebase 添加到您的网络应用

【讨论】:

  • 感谢您的快速回答。我相信我在我的问题中犯了一个错误,因为我忘了说代码实际上应该是服务器端 Node.js 代码(函数文件夹中的index.js)。但是,您的回答确实表明我有一些代码不匹配(我想我在“Web”选项卡而不是“Node.js”选项卡下复制了用于数据库初始化的代码)。我会尝试编辑代码来解决这个问题,我可能会更新问题以查看它是否解决了问题。再次感谢。
猜你喜欢
  • 2018-06-11
  • 2018-05-13
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2017-09-30
  • 2021-05-08
相关资源
最近更新 更多