javascript - 进程在完成请求之前退出 - AWS Lambdas

标签 javascript node.js amazon-web-services

我正在使用 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/

相关文章:

amazon-web-services - AWS Amplify React GET 请求错误 - 缺少身份验证 token

javascript - 使用 Jest 测试 React Native 时如何模拟 LayoutAnimation

javascript - 为什么要把 JavaScript 放在 head

javascript - jQuery .slideUp() 不工作

node.js - Sqlite3、 Electron 、webpack : cannot find module abi_crosswalk. json

node.js - 如果__dirname为您提供文件夹,那么您在什么目录中获得项目的根目录?

angularjs - Satellizer 和推特登录

amazon-web-services - 资源定义格式错误

javascript - 为什么我的代码会断开 Node 中的 Socket.io 连接?

django - 亚马逊 S3 + docker - "403 Forbidden: The difference between the request time and the current time is too large"