在我的生产环境中,我无法从我的Netlify无服务器功能返回数据。它目前正在10秒后超时,响应状态代码为502和以下错误消息:
{
"errorMessage": "2019-... 5f... Task timed out after 10.01 seconds"
}
然而,在我的开发环境中,Netlify无服务器功能工作得很好,响应于我数据库中的数据。
我已经确认,我的Netlify站点和我的.env
文件中的环境变量是相同的。我将如何调试这个问题并解决这个问题?
Netlify无服务器函数
const { connectDatabase, ProjectSchema } = require('../database')
const createHeaders = origin => {
// This is a comma-separated list of allowed origins, including my local
// environment and production website
const allowedOrigins = process.env.ALLOWED_ORIGINS
? process.env.ALLOWED_ORIGINS.split(',')
: []
return {
'Content-Type': 'application/json',
'Access-Control-Allow-Headers':
'Origin, X-Requested-With, Content-Type, Accept',
'Access-Control-Allow-Origin': allowedOrigins.includes(origin)
? origin
: ''
}
}
/**
* This function queries for all `Project` instances in the database and
* returns it in the response body.
*/
const getProjects = async origin => {
try {
const projects = await ProjectSchema.find()
return {
headers: createHeaders(origin),
statusCode: 200,
body: JSON.stringify({ projects })
}
} catch (err) {
return {
headers: createHeaders(origin),
statusCode: 400,
body: JSON.stringify({ error: err })
}
}
}
/**
* This function is the serverless lambda for `/.netlify/functions/get-projects`.
*/
exports.handler = async event => {
try {
await connectDatabase()
const response = await getProjects(event.headers.origin)
return response
} catch (err) {
return err
}
}
数据库函数
require('dotenv').config()
const mongoose = require('mongoose')
const { Schema } = mongoose
/**
* This function establishes a connection to the MongoDB Atlas database.
*/
exports.connectDatabase = async () => {
await mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
})
}
exports.ProjectSchema = mongoose.model(
'project',
new Schema({
title: {
type: String,
required: [true, 'Title field is required']
},
description: {
type: String,
required: [true, 'Description field is required']
}
})
)
从客户端获取请求
const App = () => {
const [projects, setProjects] = useState([])
useEffect(() => {
fetch(API.GET_PROJECTS, {
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(({ projects }) => setProjects(projects))
}, [])
return (...)
}
netlify.toml
[build]
functions = "functions"
package.json
{
"scripts": {
"build": "npm run build:client & npm run build:server",
"build:client": "webpack --config webpack.client.js",
"build:server": "netlify-lambda build src/server/functions -c webpack.server.js",
"start": "netlify-lambda serve src/server/functions",
"start:dev": "npm run build:client -- --watch"
}
}
发布于 2020-01-21 01:52:26
这可能有点晚了,但netlify函数有10秒的执行限制:https://docs.netlify.com/functions/overview/#default-deployment-options
他们指定,如果您需要更长的执行时间,您可以与他们的销售团队谈谈您的用例,并且他们可能能够调整它。
当您在开发环境中运行时,执行时间是多少?
发布于 2021-04-22 20:37:41
我在Mongoose和Netlify Lambda函数中也出现了这个超时问题。问题是Mongoose使数据库连接保持打开,导致Lambda函数未完成。我的解决方案是在请求结束时关闭与mongoose.disconnect()
的连接。
await mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// ... do some db stuff
// make sure you close the connection when you are done
mongoose.disconnect();
https://stackoverflow.com/questions/59330183
复制相似问题