update Operator

 avatar
unknown
javascript
24 days ago
1.1 kB
2
Indexable
async function updateProfile(body) {
  const bodyParsed = JSON.parse(body || "{}");
  const bodyData = bodyParsed.data;
  const userId = bodyParsed.user_id;
  const userRole = bodyParsed.user_role;
  console.log(bodyData);
  console.log(userId);
  console.log(userRole);
  const command = new import_lib_dynamodb4.UpdateCommand({
    TableName: "User",
    Key: { user_id: userId, user_role: userRole },
    UpdateExpression: "set " + Object.keys(bodyData).map((key) => `${key} = :${key}`).join(", "),
    ExpressionAttributeValues: Object.keys(bodyData).reduce((acc, key) => ({
      ...acc,
      [`:${key}`]: bodyData[key]
    }), {})
  });
  try {
    const response = await dynamodb4.send(command);
    console.log(response);
    return {
      statusCode: 200,
      headers: headers4,
      body: JSON.stringify({ message: "Profile updated successfully" })
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      headers: headers4,
      body: JSON.stringify({ message: "Failed to update profile, error: " + error })
    };
  }
}
Editor is loading...
Leave a Comment