gpt4 book ai didi

javascript - 获取 Firestore 文档时无法加载 'protobuf.js'

转载 作者:行者123 更新时间:2023-11-29 11:01:08 25 4
gpt4 key购买 nike

运行准系统节点文件:

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: INTERNAL UNHANDLED ERROR: Error: Failed to fetch file at /.../project/node_modules/protobufjs/dist/protobuf.js:5164:30 at ReadFileContext.callback (/.../p/node_modules/protobufjs/dist/protobuf.js:358:29) at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:420:13) (node:43981) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Failed to fetch file (node:43981) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

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

最佳答案

类似问题:

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

控制台错误(Node 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:未处理的 promise 拒绝(拒绝 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');
});
}

关于javascript - 获取 Firestore 文档时无法加载 'protobuf.js',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46817763/

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