1

I had the following code which originally worked for creating a user.

var params = {
  TableName: 'myproject-user',
  Item: {
    id: req.body.userName,
    email: req.body.email
  }
};

if(req.body.currency) {
  params.Item.currency = {
    type: req.body.currency.type,
    bank: req.body.currency.bank,
    amount: req.body.currency.amount,
  }
}

docClient.put(params, function(err, data) {
  if (err) {
    res.send({
      success: false,
      message: 'Error: ' + err
    });
  } else {
    const { Items } = data;
    res.send({
      success: true,
      message: 'Added user',
      email: req.body.email
    });
  }
});

I'm now switching to put to update, as I want to be able to keep any existing values on the object while updating the email or name. In addition I have added the following line in the params object to specify the key which is id.

Key: { id : req.user.sub },

The doc.update code executes and I get back status 200 as if there was no problem, but when I look at the table the information hasn't been updated.

Do I need to use an expression or something to get update to work?

Code with changes:

var params = {
  TableName: 'myproject-user',
  Key: {"id":req.user.sub},
  Item: {
    id: req.body.userName,
    email: req.body.email
  }
};

docClient.update(params, function(err, data) {
  if (err) {
    res.send({
      success: false,
      message: 'Error: ' + err
    });
  } else {
    const { Items } = data;
    res.send({
      success: true,
      message: 'Added user',
      email: req.body.email
    });
  }
});

1 Answer 1

1

I was able to get this working by using UpdateExpression, ExpressionAttributeValues, and ReturnValues attributes instead of Item, as described here:

var params = {
  TableName: 'krncdev-user',
  Key: {"id":req.user.sub},
  UpdateExpression: "set email=:e",
  ExpressionAttributeValues:{
      ":e": req.body.email
  },
  ReturnValues:"UPDATED_NEW"
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.