Reputation: 2860
I have to insert following data in a DynamoDB table using express framework in a node.js application
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>
I understand I need to use express body-parser
as mentioned in this post
But it is not clear to me how to create the JSON that I need to insert in a DynamoDB table using the body-parser
approach described. I can do this with jQuery to read those html items and create a JSON. For example, I need to create a JSON like below:
var paramsInsert = {
TableName:tableName,
Item:{
"email": email,
"info":{
"FirstName": fName,
"LastName": lName,
"Phone": phone,
"ZipCode": zip
}
}
};
This paramsInsert
is eventually passed to DynamoDB to insert into a table like below
insertAtTable(paramsInsert);
How do I create paramsInsert
using the body-parser
approach?
EDIT: Following this link I wrote below code but still not getting the output
app.post('/register.html', function(req, res) {
const { fname, lname, email, phone, zip } = req.body;
console.log(fname)
}
Upvotes: 0
Views: 1163
Reputation: 23052
It looks like you're missing the name
attribute on the input elements:
<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">
With the name
attribute specified, you should now be able to do the following:
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
}
}
}
This can be shorten by using ES2018's object spread:
app.post('/register.html', function(req, res) {
const paramsInsert = {
TableName: 'example',
Item: { ...req.body }
}
}
Upvotes: 2