0

当我第一次发出请求时它工作正常,但是当我再次发出请求而不重新启动服务器时,它会超时并返回一些承诺错误

following is the code and error

module.exports.getOne = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;

  db.once('open', () => {
      Client.findOne({name:{ $regex : new RegExp(event.pathParameters.name, "i") }})
        .then(client => callback(null, {
          statusCode: 200,
          body: JSON.stringify(client)
        }))
        .catch(err => callback(null, {
          statusCode: err.statusCode || 500,
          headers: { 'Content-Type': 'text/plain' },
          body: 'Could not fetch the note.'
        }))
        .finally(() => {
          // Close db connection or node event loop won't exit , and lambda will timeout
          db.close();
        });
    });
};

Error:

(node:3273) UnhandledPromiseRejectionWarning: Error: Cannot stop server while in stopping state
4

1 回答 1

1

问题是你没有通过调用来完成调用,callback所以它认为你的 getOne() 函数没有完成。

当您使用其模板设置新的无服务器应用程序时,请查看无服务器提供的示例代码

module.exports.hello = (event, context, callback) => {
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,
    }),
  };

  callback(null, response);
};
于 2018-04-16T18:35:12.517 回答