首页 > 解决方案 > 如何在回调前插入http post数据?

问题描述

我使用阿里云构建了一个API网关、函数计算和Tablestore(Nosql)来接收http post数据并将其插入到一个Nosql数据库中。

在测试我的代码时,插入函数在回调响应之前没有调用。我知道我应该使用Async/Await,但只是不完全了解如何使用它。

我试过.promise()插入函数确实调用但http的回调响应没有调用。如何将数据放入数据库并获取函数处理程序的回调?我的一些代码:

var TableStore=require('tablestore');
async function handler(event, context, callback) {
var putReq = {
    tableName: 'wifi',
    condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
    primaryKey: [{ 'date':'123'  }],
    attributeColumns: [
      {'col1': JSON.stringify(data)}, // string column
      {'col2': 100},    // integer column
    ],
  };
await client.putRow(putReq,function(err, data) {
    if (err) return callback(err);

    console.log('putRow suceess: %j', data);

      callback(null, data);
    });

   const  response = Object.assign(baseresponse, {
           body: {
                status: "ok",
                body:event.body
            }
        });

   callback(null, response);


}
module.exports.handler = handler;

标签: javascript

解决方案


推荐阅读