node.js - Firestore 查询时间戳给出“您的函数超时”
问题描述
我有一个订单请求的firestore集合,我在其中查询预订时间和状态,下面是数据库的屏幕截图
现在我基于多个子句的查询都可以正常工作,但是每当我尝试查询时间戳时,它都会失败,并且出现此错误:- Your function timed out after ~60s. To configure this timeout
。我试图用谷歌搜索如何添加时间戳查询,所有这些都暗示了我迄今为止一直在使用的相同的东西。下面是我的代码这是init.ts
文件:-
const admin = require('firebase-admin');
import * as functions from 'firebase-functions';
admin.initializeApp();
export const db = admin.firestore();
export const timestamps = admin.firestore.Timestamp.now();
functions.logger.debug(`timestamps of server .== ${timestamps.seconds}`);
export const auth = admin.auth();
这就是我获取订单的方式,所以它基本上基于状态和时间戳
get-order.ts
import {db, timestamps} from "../init";
const express = require('express');
import * as functions from 'firebase-functions';
import 'firebase/firestore'
import {getUserCredentialsMiddleware} from "../auth.middleware";
const bodyParser = require('body-parser');
const cors = require('cors');
export const getOrderDetails = express();
const date = timestamps.seconds;
getOrderDetails.use(bodyParser.json());
getOrderDetails.use(cors({origin:true}));
getOrderDetails.use(getUserCredentialsMiddleware);
getOrderDetails.get('/', async (req, res) => {
try {
functions.logger.debug(`Calling getOrderDetails try.== ${req.body}`);
db.collection('orderRequest')
.where('status', '==', 'Pending')
.where('meta.booking_time', '<', date)
.get().then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
return res.json(documentSnapshot)
});
})
}
catch(err) {
functions.logger.error(`Could not getOrderDetails.`, err);
res.status(500).json({message: "Could not getOrderDetails."});
}
});
我的index.ts
档案
import * as functions from "firebase-functions";
import { getOrderDetails } from "./order-request/get-orders";
export const getRecentOrders =
functions.https.onRequest(getOrderDetails);
因此,现在每当我删除时间戳查询时,它都可以正常工作并立即得到响应,但是当我将其添加回来时,它会给我超时错误,所以我的问题是带有时间戳的查询是否耗时?如果我的订单清单很大怎么办?查询时间戳是一个可行的选择吗?以及我做错了什么,为什么我没有得到订单详细信息,因为它在任何地方的解释都是一样的。
解决方案
这里有两个问题。
首先,如果查询结果为空,您的函数不会向客户端发送任何内容。如果您不向客户端发送响应,该函数将超时。如果没有匹配的文档,您将必须明确决定并编码要发送的内容。
其次,res.json()
在您确实有多个文档的情况下,您会多次调用(在循环内)。您应该只res.json()
使用完整的对象调用一次以发送回客户端。
推荐阅读
- javascript - 浏览器通知可判定操作不起作用(javascript、vue.js) - 不再受支持吗?
- python - 从 IGraph (Python) 中的多个文件中读取图形
- html - 4列全角链接部分
- regex - 仅匹配第一次出现的正则表达式不起作用
- vba - 选择与否...单词自动删除句子后的空格
- debugging - 如何将 Xamarin.Android 输出写入 Windows 文本文件?
- sql - SQL 事务死锁
- security - 如何保护用户生成和使用的文件中的 IP 敏感数据
- python - Python:添加作为组第一行的列
- wordpress - wordpress:不同的页面有不同的菜单布局和位置