【问题标题】:Unable to load 'protobuf.js' when fetching Firestore document获取 Firestore 文档时无法加载“protobuf.js”
【发布时间】:2018-03-30 18:36:39
【问题描述】:

运行准系统节点文件:

const firebase = require("firebase");
const http = require('http')

require("firebase/firestore");

firebase.initializeApp({
  apiKey: '...',
  authDomain: '...',
  databaseURL: '...',
  serviceAccount: '...',
  projectId: '...'
});

var db = firebase.firestore();
var userRef = db.collection('...').doc('...');

导致以下终端规则

Firestore (4.5.2) 2017-10-18T19:16:47.719Z:内部未处理错误: 错误:无法获取文件 在 /.../project/node_modules/protobufjs/dist/protobuf.js:5164:30 在 ReadFileContext.callback (/.../p/node_modules/protobufjs/dist/protobuf.js:358:29) 在 FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:420:13) (node:43981) UnhandledPromiseRejectionWarning: Unhandled Promise 拒绝(拒绝 id:1):错误:无法获取文件(节点:43981) [DEP0018] 弃用警告:未处理的承诺拒绝是 已弃用。将来,未处理的承诺拒绝 将使用非零退出代码终止 Node.js 进程。

但我检查了 node_modules 文件夹,它就在那里。我也试过重装了,其他人好像对当前版本的protobuf.js没有问题。

【问题讨论】:

  • 我今天遇到了同样的问题。在下面添加了答案以显示类似的方法并记录输出和配置。

标签: javascript firebase npm google-cloud-firestore


【解决方案1】:

类似问题:

const firebase = require('firebase');
require("firebase/firestore");
const config = {
  apiKey: "-redacted-",
  authDomain: "-redacted-",
  databaseURL: "-redacted-",
  projectId: "-redacted-",
  storageBucket: "-redacted-",
  messagingSenderId: "-redacted-"
};
firebase.initializeApp(config);
let db = firebase.firestore();
let ref=db.collection('example').doc('test');
ref.set({test:true});

控制台错误(节点 v6.11.4):

Firestore (4.5.2) 2017-10-18T19:48:00.297Z: INTERNAL UNHANDLED ERROR:  Error: Failed to fetch file
at Error (native)
at /Users/krispy.uccello/Development/devrel/constellation/functions/node_modules/protobufjs/dist/protobuf.js:5164:30
at ReadFileContext.callback (/Users/krispy.uccello/Development/devrel/constellation/functions/node_modules/protobufjs/dist/protobuf.js:358:29)
at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:367:13)

(节点:71564)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):错误:无法获取文件

数据库规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

更新: 此问题似乎仅与写入有关。我能够毫无问题地从 Firestore 读取数据。

更新 #2: 我写了一个firebase函数来处理写操作并且它工作。该问题可能与客户端安装的 protobuf js 的已发布版本有关。不确定 Firebase 端使用的是什么版本,但它似乎可以工作。请参阅以下通过 firebase https 触发器调用的函数 - 它可以工作并且数据会出现在 firestore 数据库中。

function writeWorkerProfile(id, data, res) {
  let ref = 
  db.collection('scratch').doc('v1').collection('workers').doc(`${id}`);
  ref.set(data)
  .then(function() {
    console.log("Document successfully written!");
    res.status(200).send({ok:true})
  })
  .catch(function(error) {
      console.error("Error writing document: ", error);
      res.status(500).send('Error occurred: Could not write data');
  });
}

【讨论】:

猜你喜欢
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
相关资源
最近更新 更多