Sco*_*t D 14 javascript firebase google-cloud-functions google-cloud-firestore
我从Firestore文档中获取了一个示例函数,并且能够从我的本地firebase环境成功运行它.但是,一旦我部署到我的firebase服务器,该功能就完成了,但是没有在firestore数据库中创建任何条目.firebase功能日志显示"截止日期已超过".我有点困惑.任何人都知道为什么会这样,以及如何解决这个问题?
这是示例函数:
exports.testingFunction = functions.https.onRequest((request, response) => {
var data = {
name: 'Los Angeles',
state: 'CA',
country: 'USA'
};
// Add a new document in collection "cities" with ID 'DC'
var db = admin.firestore();
var setDoc = db.collection('cities').doc('LA').set(data);
response.status(200).send();
});
Run Code Online (Sandbox Code Playgroud)
小智 9
Firestore有限制.
由于其限制,可能会发生"超出截止日期".
看到这个.https://firebase.google.com/docs/firestore/quotas
每秒1个文档的最大写入速率
https://groups.google.com/forum/#!msg/google-cloud-firestore-discuss/tGaZpTWQ7tQ/NdaDGRAzBgAJ
我写了这个小脚本,它使用批量写入(最多 500 个)并且只写一个批次。
通过首先创建一个 batchWorker 来使用它,let batch: any = new FbBatchWorker(db);
然后向 worker 中添加任何内容batch.set(ref.doc(docId), MyObject);
。并通过batch.commit()
. api 与普通 Firestore Batch ( https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes ) 相同,但是,目前它只支持set
.
import { firestore } from "firebase-admin";
class FBWorker {
callback: Function;
constructor(callback: Function) {
this.callback = callback;
}
work(data: {
type: "SET" | "DELETE";
ref: FirebaseFirestore.DocumentReference;
data?: any;
options?: FirebaseFirestore.SetOptions;
}) {
if (data.type === "SET") {
// tslint:disable-next-line: no-floating-promises
data.ref.set(data.data, data.options).then(() => {
this.callback();
});
} else if (data.type === "DELETE") {
// tslint:disable-next-line: no-floating-promises
data.ref.delete().then(() => {
this.callback();
});
} else {
this.callback();
}
}
}
export class FbBatchWorker {
db: firestore.Firestore;
batchList2: {
type: "SET" | "DELETE";
ref: FirebaseFirestore.DocumentReference;
data?: any;
options?: FirebaseFirestore.SetOptions;
}[] = [];
elemCount: number = 0;
private _maxBatchSize: number = 490;
public get maxBatchSize(): number {
return this._maxBatchSize;
}
public set maxBatchSize(size: number) {
if (size < 1) {
throw new Error("Size must be positive");
}
if (size > 490) {
throw new Error("Size must not be larger then 490");
}
this._maxBatchSize = size;
}
constructor(db: firestore.Firestore) {
this.db = db;
}
async commit(): Promise<any> {
const workerProms: Promise<any>[] = [];
const maxWorker = this.batchList2.length > this.maxBatchSize ? this.maxBatchSize : this.batchList2.length;
for (let w = 0; w < maxWorker; w++) {
workerProms.push(
new Promise((resolve) => {
const A = new FBWorker(() => {
if (this.batchList2.length > 0) {
A.work(this.batchList2.pop());
} else {
resolve();
}
});
// tslint:disable-next-line: no-floating-promises
A.work(this.batchList2.pop());
}),
);
}
return Promise.all(workerProms);
}
set(dbref: FirebaseFirestore.DocumentReference, data: any, options?: FirebaseFirestore.SetOptions): void {
this.batchList2.push({
type: "SET",
ref: dbref,
data,
options,
});
}
delete(dbref: FirebaseFirestore.DocumentReference) {
this.batchList2.push({
type: "DELETE",
ref: dbref,
});
}
}
Run Code Online (Sandbox Code Playgroud)
根据我自己的经验,当您尝试使用不良的互联网连接编写文档时,也会发生此问题。
我使用类似于 Jurgen 建议的解决方案一次性插入小于 500 的文档,如果我使用不太稳定的 wifi 连接,则会出现此错误。当我插入电缆时,具有相同数据的相同脚本运行而没有错误。
归档时间: |
|
查看次数: |
8779 次 |
最近记录: |