火力地堡数据库查询抽出太多的时间在云功能
问题描述:
我用的NodeJS作为托管在火力功能来查询我的火力数据库后端的,我有这样的代码:火力地堡数据库查询抽出太多的时间在云功能
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors')({origin: true});
admin.initializeApp(functions.config().firebase);
const express = require('express')
exports.getAjax = functions.https.onRequest((request, response) => {
cors(request, response,() => {
console.log("request.body :", JSON.stringify(request.body));
console.log("request.query :", JSON.stringify(request.query));
var date = {
startDate: request.body.startDate,
endDate: request.body.endDate
}
var db = admin.database();
var logsDbPath = 'logs';
var usersDbPath = 'users';
var ref = db.ref(logsDbPath);
var tags;
db.ref(usersDbPath).once('value').then(function(tagsSnapshot) {
tagsSnapshot.forEach(function(tagSnapshot) {
var tagId = tagSnapshot.key;
tagSnapshot.forEach(function(sessSnapshot) {
var userSessId = sessSnapshot.key;
var userInfo = sessSnapshot.val();
});
});
tags = JSON.parse(JSON.stringify(tagsSnapshot.val()));
console.log(tags);
});
});
});
我的主要问题是,console.log(tags);
,实际上完成查询并获取标签值了〜38秒,在此情况下(有时需要约1分钟),我就会把图片波纹管:
而该对象这并不是说大可言,像100行,4页我不认为它应该花费那么多,可能我做错了什么,但是在哪里?我还必须编写一个承诺或回调,以便在前端发送该标签var响应,导致函数完成之前,我得到任何价值的标签(见图片),我来自一个PHP背景和这个异步JavaScript的概念,这对我来说是非常新的。
答
您没有向客户端发回任何响应,这意味着该函数将保持运行状态直到超时。从它的很难说多少
exports.getAjax = functions.https.onRequest((request, response) => {
cors(request, response,() => {
console.log("request.body :", JSON.stringify(request.body));
console.log("request.query :", JSON.stringify(request.query));
var date = {
startDate: request.body.startDate,
endDate: request.body.endDate
}
var db = admin.database();
var logsDbPath = 'logs';
var usersDbPath = 'users';
var ref = db.ref(logsDbPath);
var tags;
db.ref(usersDbPath).once('value').then(function(tagsSnapshot) {
tagsSnapshot.forEach(function(tagSnapshot) {
var tagId = tagSnapshot.key;
tagSnapshot.forEach(function(sessSnapshot) {
var userSessId = sessSnapshot.key;
var userInfo = sessSnapshot.val();
});
});
tags = JSON.parse(JSON.stringify(tagsSnapshot.val()));
console.log(tags);
// Send the response to the client, which also ends the function
response.status(200).send(tags);
});
});
});
旁白:
为了确保功能只运行,只要需要,发送一个响应时已加载数据的客户端。请记住,云功能处于测试阶段,并且不断变化,我们无法知道这是冷启动还是热启动,我们无法看到您的项目检查发生了什么。
如果您想在此获得更多帮助,我建议您尝试在更容易排除故障的环境中重现问题。例如,您可以使用本地node.js进程重现perfor问题吗?
非常感谢你的回复,这真是太好了,我真的很喜欢firebase,所有的尊重你的团队!回到代码,我有response.status(200).send(JSON.stringify(tags));最后,但我的错误是我把它放在函数范围之外,函数完成了,我没有任何对象,但移动响应发送函数内部使它工作,小修改,出于某种原因,我得到Cors错误,如果我res.status(200)。发送(标签);但我改变了response.status(200).send(tags);和工作,关于查询速度现在看起来不错,我会更多地测试,谢谢! –
错字很好。我更新了我的答案以解决问题。 –