use*_*230 6 amazon-web-services express amazon-dynamodb
我的 hashkey 是一个名为 pid 的数字
这是代码
app.get('/register', (req, res) => {
var params = {
TableName:"passengers",
Item: {
"pid": 55
}
};
console.log("Adding a new item...");
docClient.put(params, function(err, data) {
if (err) {
console.log("errrrrrrr");
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("succccccc");
console.log("Added item:", JSON.stringify(data, null, 2));
}
});
console.log("Added a new item...");
res.send('<h1>some html</h1>');
})
Run Code Online (Sandbox Code Playgroud)
errrrr 或 succcc 都不会打印在日志中,但会打印“添加新项目”和“添加新项目”。
正如评论中提到的,putAPI 是异步的。因此,当 put 成功或失败时,您应该将响应返回给客户端。
请包含res.send在回调方法中。另外,请使用 HttpMethod POST来放置项目,而不是GET方法。我希望您只是将它用于快速测试目的。
console.log("Adding a new item...");
docClient.put(params, function(err, data) {
if (err) {
console.log("errrrrrrr");
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
res.send('<h1>some error html</h1>');
} else {
console.log("succccccc");
console.log("Added item:", JSON.stringify(data, null, 2));
res.send('<h1>some html</h1>');
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1789 次 |
| 最近记录: |