首页 > 解决方案 > 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);

因此,现在每当我删除时间戳查询时,它都可以正常工作并立即得到响应,但是当我将其添加回来时,它会给我超时错误,所以我的问题是带有时间戳的查询是否耗时?如果我的订单清单很大怎么办?查询时间戳是一个可行的选择吗?以及我做错了什么,为什么我没有得到订单详细信息,因为它在任何地方的解释都是一样的。

标签: node.jsfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


这里有两个问题。

首先,如果查询结果为空,您的函数不会向客户端发送任何内容。如果您不向客户端发送响应,该函数将超时。如果没有匹配的文档,您将必须明确决定并编码要发送的内容。

其次,res.json()在您确实有多个文档的情况下,您会多次调用(在循环内)。您应该只res.json()使用完整的对象调用一次以发送回客户端。


推荐阅读