最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Updating an attribute in DynamoDB that contains hyphen or dash (-) in javascript - Stack Overflow

programmeradmin4浏览0评论


in dynamoDB I have a table that has attributes that are hyphenated. (e.g. first-name)

Now I want to update them using javascript. This is my code so far:

//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));
          }
      });

However this throws me this error:

Unable to update item. Error JSON: {
  "message": "Invalid UpdateExpression: Syntax error; token: \"-\", near: \"first-name\""

Is there any way around this?
Thank you for your help :)


in dynamoDB I have a table that has attributes that are hyphenated. (e.g. first-name)

Now I want to update them using javascript. This is my code so far:

//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));
          }
      });

However this throws me this error:

Unable to update item. Error JSON: {
  "message": "Invalid UpdateExpression: Syntax error; token: \"-\", near: \"first-name\""

Is there any way around this?
Thank you for your help :)

Share Improve this question asked Sep 6, 2016 at 11:58 weggi_swaweggi_swa 3301 gold badge5 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 23

When you use attributes that contain reserved words, spaces or special characters you have to use placeholders. Take a look at documentation.

In the updateExpression instead of first-name you can use, for example, #fn placeholder and then define ExpressionAttributeNames:

ExpressionAttributeNames: {
    "#fn":"first-name"
}
发布评论

评论列表(0)

  1. 暂无评论