I have started working on dynamodb and nodeJs. I want to create an admin panel for adding new item in which primary and sort keys will be same.
For example: ADD URL is abc.com/admin/add/xyz/cdef/123
I have created an api for the same, also I am using 'put' for creating new item however it is replacing the existing one instead of creating new.
Model Code:
abc.addData= function(newData, callback){
var params = {
TableName: tableconcept,
Item: newData
}
docClient.put(params, function(err, data) {
if (err) {
console.error("Unable to create post", ". Error JSON:",
JSON.stringify(err, null, 2));
} else {
// console.log("Added item:", JSON.stringify(data, null, 2));
callback(null, data.Items);
return true;
}
});
}
Routes Code:
router.post('/', function(req, res){
var data = req.body;
var newData = new Object()
newData.data = data ;
abc.addData(newData, function(err, data){
if (err){
return res.status(401).json({
status:'failed',
message: "unable to create post"
});
}
return res.status(200).json({
status:'success',
message: 'Successfully Created post.'
});
})
});