我必须使用node.js应用程序中的express框架在DynamoDB表中插入以下数据
register.html
<input type = "text" placeholder="First Name" id = "txtFirstName"><br><br>
<input type = "text" placeholder="Last Name" id = "txtLastName"><br><br>
<input type = "email" placeholder="Email" id = "txtEmail"><br><br>
<input type = "text" placeholder="Phone" id = "txtPhone"><br><br>
<input type = "text" placeholder="Zip Code" id = "txtZip"><br><br>
我理解我需要使用this post
中提到的快速body-parser
但我不清楚如何使用所描述的body-parser
方法创建我需要在DynamoDB表中插入的JSON。我可以用jQuery来读取这些html项并创建一个JSON。例如,我需要创建一个如下所示的JSON:
var paramsInsert = {
TableName:tableName,
Item:{
"email": email,
"info":{
"FirstName": fName,
"LastName": lName,
"Phone": phone,
"ZipCode": zip
}
}
};
此paramsInsert
最终传递给DynamoDB以插入如下表
insertAtTable(paramsInsert);
如何使用paramsInsert
方法创建body-parser
?
编辑: 关注this link我写了下面的代码,但仍然没有得到输出
app.post('/register.html', function(req, res) {
const { fname, lname, email, phone, zip } = req.body;
console.log(fname)
}
答案 0 :(得分:1)
您似乎错过了输入元素的name
属性:
<input type = "text" placeholder="First Name" id = "txtFirstName" name="firstName">
<input type = "text" placeholder="Last Name" id = "txtLastName" name="lastName">
<input type = "email" placeholder="Email" id = "txtEmail" name="email">
<input type = "text" placeholder="Phone" id = "txtPhone" name="phone">
<input type = "text" placeholder="Zip Code" id = "txtZip" name="zip">
指定name
属性后,您现在应该可以执行以下操作:
app.post('/register.html', function(req, res) {
const {
firstName,
lastName,
email,
phone,
zip
} = req.body
const paramsInsert = {
TableName: 'example',
Item: {
firstName,
lastName,
email,
phone,
zip
}
}
}
可以缩短时间
app.post('/register.html', function(req, res) {
const paramsInsert = {
TableName: 'example',
Item: { ...req.body }
}
}