documentclient put is not working, tried everything

Viewed 1593

My hashkey is a number with the name pid

Here is the code

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>');


})

Neither errrr or succcc gets printed in the logs but both "Adding a new item" and "Added a new item" get printed.

1 Answers

As mentioned in comments, the put API is asynchronous. So, you should return the response to client when the put is successful or failed.

Please include the res.send inside the call back method. Alos, please use HttpMethod POST for putting the item rather than GET method. I hope you have just used it for quick testing purpose.

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>');
  }
});
Related