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 に答える