为什么我的 Vercel 无服务器功能无法在生产环境中运行?

问题描述 投票:0回答:2

您好,当我在 vercel 中部署应用程序时遇到问题,我正在创建一个没有服务器的函数,该函数使用 mongoose 为我带来 mongodb 中集合的数据。在本地,一切正常,但当我部署该功能时,它给了我这个错误:

504: GATEWAY_TIMEOUT
Code: FUNCTION_INVOCATION_TIMEOUT
ID: gru1 :: 594vg-1620811347355-66f311d5c992

我了解到 vercel 的免费计划允许您创建最多等待 10 秒的函数,这就是我收到错误的原因吗?或者可能是由于以下原因之一:https://vercel.com/support/articles/why-does-my-serverless-function-work-locally-but-not-when-deployed

无服务器功能:

import mongoose from "mongoose";
module.exports = function(req, res) {
  const Schema = mongoose.Schema;
  const inmuebleSchema = new Schema({
    user_id: String, //{type: Schema.Types.ObjectId, ref: 'User'}
    desc: String,
    tipo_propiedad: { type: String, enum: ["casa", "quinta", "departamento"] },
    amb: Number,
    estado: { type: String, enum: ["venta", "alquiler"] },
    banios: Number,
    dorm: Number,
    coch: Number,
    direc: String,
    precio: String,
    images: { type: Array, default: [] },
    uploadedDate: { type: Date, default: Date.now() }
  });
  const Inmueble = mongoose.model("inmueble", inmuebleSchema);
  mongoose
    .connect(process.env.MONGODB_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true
    })
    .then(() => {
      Inmueble.find()
        .exec()
        .then(inmuebles => res.json({ inmuebles }))
        .catch(err => res.json({ status: "error" }));
    })
    .catch(err => {
      res.status(500);
    });
};
serverless vercel
2个回答
0
投票

问题是,我没有将站点的 IP 添加到集群的白名单中。


0
投票

它有一个简单的解决方案:转到项目中的 MongoDB,在安全选项卡中查找网络访问,

  1. 添加IP地址
  2. 允许从任何地方访问
  3. 确认

这解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.