javascript - 如何在回调前插入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;
解决方案
推荐阅读
- angular - 有没有办法在 Angular 7 中为模板 html 属性 *key* 使用变量?
- java - 如何使用eclipse在java中打印带下划线的字符串
- go - httputil.NewSingleHostReverseProxy 调用会增加内存使用量
- python-3.x - 从 Tkinter 中的回调函数中检索信息
- python - 创建时将用户设置为组
- swift - 如何获取要正确更新的实体数量
- python - 当键对相同时,将值从一个嵌套字典复制到另一个嵌套字典
- python - 是否可以更改您在 python 中使用的架构?
- javascript - 在不活动时结束 Zopim 聊天
- c# - DataGridView 性能改进