2016-09-06 30 views
0


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

然而,這將引發我這個錯誤:

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

有沒有解決這個辦法嗎?
感謝您的幫助:)

回答

1

當您使用包含保留字,空格或特殊字符的屬性時,您必須使用佔位符。看看documentation

updateExpression而不是first-name可以使用,例如,#fn佔位符,然後定義ExpressionAttributeNames

ExpressionAttributeNames: { 
    "#fn":"first-name 
} 
+0

真棒,這個工程。謝謝....我從來沒有在文檔中找到過 –

相關問題