Node.js更新DynamoDB数据报错:Syntax error; token: "updated", near: "set updated ="
updated Field Hey there, let's break down this syntax error you're seeing with DynamoDB's updated field update. That error message Syntax error; token: "updated", near: "set updated =" almost always stems from a misformatted UpdateExpression or missing required parameters in your params object. Let's walk through the most common fixes and best practices:
Common Root Causes & Fixes
1. Missing Placeholder Values in ExpressionAttributeValues
DynamoDB requires you to use placeholder variables (prefixed with :) in your UpdateExpression instead of hardcoding values directly. If you reference a placeholder but don't define it in ExpressionAttributeValues, you'll get this syntax error.
Wrong Example:
// ❌ Error: Missing ExpressionAttributeValues for the updated value const params = { TableName: "YourTableName", Key: { "primaryKey": "targetRecordID" }, UpdateExpression: "SET updated = '2024-05-20', newField = 'newData'" // Direct values cause syntax issues };
Corrected Example:
// ✅ Correct: Uses placeholders and defines them in ExpressionAttributeValues const params = { TableName: "YourTableName", Key: { "primaryKey": "targetRecordID" }, UpdateExpression: "SET updated = :updatedVal, newField = :newFieldVal", ExpressionAttributeValues: { ":updatedVal": new Date().toISOString(), // Example: ISO timestamp for updated time ":newFieldVal": "Your additional data here" }, ReturnValues: "ALL_NEW" // Optional: Returns the full updated item for verification };
2. Typos or Invalid Syntax in UpdateExpression
Double-check that your SET clause has proper spacing and syntax:
- Ensure there's a space after
SETand between each field assignment (e.g.,SET updated = :val, field2 = :val2notSETupdated=:val). - If your field name had special characters (not the case here with
updated), you'd need to wrap it in#and define it inExpressionAttributeNames, butupdatedis a valid, non-reserved field name so this isn't needed.
3. Missing or Incorrect Primary Key in Key
DynamoDB can only update records if you specify the full primary key (partition key + sort key if your table uses one) in the Key property of your params. Omitting this won't cause the exact syntax error you're seeing, but it's a common adjacent issue to check once the syntax is fixed.
For Python SDK Users (if applicable)
If you're using boto3, the structure is similar but uses snake_case for parameters:
import boto3 dynamodb = boto3.resource("dynamodb") table = dynamodb.Table("YourTableName") response = table.update_item( Key={"primary_key": "targetRecordID"}, UpdateExpression="SET updated = :updated_val, new_field = :new_field_val", ExpressionAttributeValues={ ":updated_val": "2024-05-20T12:34:56Z", ":new_field_val": "Additional data" }, ReturnValues="UPDATED_NEW" )
Quick Troubleshooting Checklist
- Verify every placeholder in
UpdateExpressionhas a matching entry inExpressionAttributeValues. - Ensure no hardcoded values are directly in the
UpdateExpression(always use placeholders). - Check for typos in field names or keywords (e.g.,
SETspelled correctly, spaces in the right places).
内容的提问来源于stack exchange,提问作者ntruiz

