gpt4 book ai didi

javascript - Firestore 执行 get 命令 super 慢

转载 作者:行者123 更新时间:2023-11-28 03:35:01 26 4
gpt4 key购买 nike

我想从 Firestore 中的文档中查询数据以获取索引号,并将其设置为新文档,还将新的索引号设置回查询的文档,但是当调用我的函数时,它说仅查询一个文档就花费了 131909 毫秒doc,该集合中的文档少于 10 个。

这是我的代码

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const request = require('request');
const region = 'asia-east2';
const runtimeOpts = {
timeoutSeconds: 4,
memory: "2GB"
};
admin.initializeApp(functions.config().firebase)
const db = admin.firestore();

exports.Leave_request = functions.region(region).runWith(runtimeOpts).https.onRequest((request,respond) => {
var userID = request.body.userID;
var profileImage = request.body.profileImage;
var name = request.body.name;
var email = request.body.email;
var office = request.body.office;
var department = request.body.department;
var workplace = request.body.workplace;
var leavetype = request.body.leavetype;
var startdate = request.body.startdate;
var enddate = request.body.enddate;
var reason = request.body.reason;
var num = 0
var newnum = 0
var LeaveID = ""
var getNum = db.collection("Leave_list").doc("Count").get().then(replydoc => {
if (!replydoc.exists){
console.log("Not exists")
} else {

num = replydoc.data().NumList
newnum = num + 1;
LeaveID = "L" + newnum;
const setData = db.collection("Leave_list").doc(LeaveID).set({
"userID": userID,
"profileImage": profileImage,
"name": name,
"email": email,
"office": office,
"department": department,
"workplace": workplace,
"leavetype": leavetype,
"startdate": startdate,
"enddate": enddate,
"reason": reason,
"status": "Pending"
})
var setNum = db.collection("Leave_list").doc("Count").set({
"NumList": newnum
})
push_leave(userID, profileImage, name, email, office, department, workplace, LeaveID, leavetype, startdate, enddate, reason, "Waiting", "#ffc400")
}
return null
})
var getAdminUID = db.collection("Leave_admin").where("Headof", "==", "ABCD").limit(1).get().then(snapshort => {
if (snapshort.empty){
console.log("No matching documents")
} else {
snapshort.forEach(doc => {
var adminID = doc.id
push_admin(adminID, profileImage, name, email, office, department, workplace, LeaveID, leavetype, startdate, enddate, reason, "Waiting")
})
}
return null
}).catch(err => {
console.log(err);
})

respond.send("{}")
function push_leave(toUID, toProfileImage, toName, toEmail, toOffice, toDepartment, toWorkplace, toLeaveID, toLeavetype, toStartdate, toEnddate, toReason, toStatus, toStatusColor) {
//Do the post Request
}
function push_admin(toUID, toProfileImage, toName, toEmail, toOffice, toDepartment, toWorkplace, toLeaveID, toLeavetype, toStartdate, toEnddate, toReason, toStatus) {
//Do the post Request
}

我注意到cloud firestore的位置离我的国家较远,但我认为这不会对查询时间产生太大影响,也许还有其他因素。

有人有办法让它回复更快吗?

最佳答案

您的代码正在立即发送响应。它没有正确使用 promise 来等待数据库操作完成后再发送响应。如果您在所有异步工作完成之前发送响应,它可能会提前终止您的函数,取消任何挂起的异步操作。

关于javascript - Firestore 执行 get 命令 super 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57826481/

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