即使用户回调后,lambda函数也始终返回函数超时(null,响应)

时间:2020-03-30 04:53:07

标签: node.js aws-lambda

const mongo = require('mongoose');
const MONGODB_URI = 'mongodb atles url connection string'
const db = require('./model')

exports.main = (event, context, callback) => {
    mongo.connect(MONGODB_URI,{ useNewUrlParser: true, useUnifiedTopology: true })
    mongo.connection.once('open',async ()=>{enter code here
        console.log('SUCCESSFUL DB CONNECTION!!')
        putData();
    }).on('error',(err)=>{
        console.log('ERROR IN DATABASE CONNECTION!!',err)
    })

    const putData = async ()=>{
        const res = await db.create({
                updated_at:new Date(),
                user_id:event.user_id,
                question_id:event.question_id,
            })
            console.log(res)
            callback(null, res)
    }
}

一切正常,在回调给出输出之前,数据保存到数据库,甚至控制台日志也是如此。但是回调不起作用。.像这样callback('error') ...

这是日志。

2020-03-30T04:48:59.035Z    7e148909-607d-4c71-9c07-3c45827c5b5c    INFO    SUCCESSFUL DB CONNECTION!!
2020-03-30T04:48:59.413Z    7e148909-607d-4c71-9c07-3c45827c5b5c    INFO    {
  updated_at: 2020-03-30T04:48:59.072Z,
  _id: 5e817a3b36599000080f69fb,
  user_id: '2637128638162391923',
  question_id: '123812u31',
  __v: 0
}
END RequestId: 7e148909-607d-4c71-9c07-3c45827c5b5c
REPORT RequestId: 7e248909-607d-4c71-9c07-3c45827c5b5c  Duration: 3003.30 ms    Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 94 MB  Init Duration: 516.80 ms    
2020-03-30T04:49:00.979Z 7e248909-607d-4c71-9c07-3c45827c5b5c Task timed out after 3.00 seconds

1 个答案:

答案 0 :(得分:0)

函数将继续执行,直到事件循环为空或函数超时为止。因此,响应将被延迟,直到事件循环中的所有任务完成为止。

您可以将context.callbackWaitsForEmptyEventLoop = false设置为立即发送回复。

更多信息here