Javascript Firestore执行get命令的速度非常慢
我想从Firestore中的文档中查询索引号的数据,并将其设置为新文档。我还想将新索引号设置回文档查询源,但调用我的函数时,它说仅查询一个文档需要131909毫秒,该集合中的文档不到10个 这是我的密码Javascript Firestore执行get命令的速度非常慢,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,我想从Firestore中的文档中查询索引号的数据,并将其设置为新文档。我还想将新索引号设置回文档查询源,但调用我的函数时,它说仅查询一个文档需要131909毫秒,该集合中的文档不到10个 这是我的密码 const functions = require('firebase-functions'); const admin = require('firebase-admin'); const request = require('request'); const region = 'asia-e
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
}
我注意到云firestore的位置离我的国家更远,但我认为这不会对查询时间造成太大影响,可能还有其他因素
有人想让它回复得更快吗?您的代码会立即发送响应。在发送响应之前等待数据库操作完成是不正确的。如果在所有异步工作完成之前发送响应,它可能会提前终止函数,取消任何挂起的异步操作 您的代码将立即发送响应。在发送响应之前等待数据库操作完成是不正确的。如果在所有异步工作完成之前发送响应,它可能会提前终止函数,取消任何挂起的异步操作 你标记了这个谷歌云功能,那么你有完整的功能共享吗?在更高的层次上,您可能没有正确地执行某些操作。请编辑该问题以显示所有相关代码。@DougStevenson当然,我已经编辑了它。您标记了这个google云函数,那么您有完整的函数要共享吗?在更高的层次上,您可能没有正确地执行某些操作。请编辑问题以显示所有相关代码。@DougStevenson当然,我已经编辑好了,我明白了。我认为在自动发送响应之前,它将等待数据库操作完成。现在它的回复速度要快得多。谢谢,好的,我明白了。我认为在自动发送响应之前,它将等待数据库操作完成。现在它的回复速度要快得多。非常感谢。