在JavaScript中更新包含连字符或短划线( - )的DynamoDB中的属性

weg*_*swa 6 javascript node.js amazon-dynamodb


在dynamoDB中,我有一个具有连字符属性的表.(例如名字)

现在我想用javascript更新它们.到目前为止这是我的代码:

//create UpdateExpression and ExpressionAttributeValues
    let updateExpression = "set ";
    let expressionAttributeValues ={};
    if (e.firstName !== null){
        updateExpression = updateExpression + " "+ 'first-name'+" = :f,";
        expressionAttributeValues[":f"] = e.firstName;
    }

    let table = "tableName";
    let bpNumber = e.bpNumber;
    let params = {
        TableName: table,
        Key: {
            "bpNumber": bpNumber
        },
        UpdateExpression: updateExpression,
          ExpressionAttributeValues: expressionAttributeValues,
          ReturnValues:"UPDATED_NEW"

    };

    console.log("Updating the item...");
      docClient.update(params, function(err, data) {
          if (err) {
              console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
          } else {
              console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
          }
      });
Run Code Online (Sandbox Code Playgroud)

但是这会引发我这个错误:

Unable to update item. Error JSON: {
  "message": "Invalid UpdateExpression: Syntax error; token: \"-\", near: \"first-name\""
Run Code Online (Sandbox Code Playgroud)

有没有办法解决?
谢谢您的帮助 :)

Mac*_*och 11

使用包含保留字,空格或特殊字符的属性时,必须使用占位符.看看文档.

updateExpression代替first-name你可以使用,例如,#fn占位符,然后定义ExpressionAttributeNames:

ExpressionAttributeNames: {
    "#fn":"first-name"
}
Run Code Online (Sandbox Code Playgroud)