我正在使用 AWS Lambdas 尝试连接到 CMS,但遇到了以下错误:
Process exited before completing request
下面是我的代码片段:
require('dotenv').config({ silent: true });
const contentful = require('contentful');
exports.handler = (event, context) => {
const client = contentful.createClient({
space: process.env.SPACE_ID,
accessToken: process.env.CDA_TOKEN
})
client.getEntries({
'content_type': 'thumbnail'
})
.then(function (entries) {
context.succeed(JSON.stringify(entries));
})
};
此错误是否表明我在代码中的某个位置出现了错误,导致其无法运行 context.succeed 或者我错误地使用了 context.succeed?
最佳答案
Process exited before completing request
这意味着您遇到了未处理的异常。您的处理程序基本上崩溃了,而没有告诉 Lambda 原因。
查看您的代码,很可能 client.getEntries()
Promise 被拒绝,并且您没有为您的 Promise 提供 .catch()
.
您可以执行以下操作...
// Use callback coz context.succeed() is soooo legacy.
exports.handler = (event, context, callback) => {
const client = contentful.createClient({
space: process.env.SPACE_ID,
accessToken: process.env.CDA_TOKEN
})
return client.getEntries({
'content_type': 'thumbnail'
})
// Be consistent with arrow function usage.
.then((entries) => callback(null, JSON.stringify(entries)))
// This is what is missing.
.catch((err) => {
// Log the error so you know what it is and fix it.
console.error(err);
// Be polite and tell Lambda that the invocation failed.
callback(err);
});
};
关于javascript - 进程在完成请求之前退出 - AWS Lambdas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49938611/