2016-12-27 5 views
0

Ich möchte eine Funktion schreiben, die angegebene Parameter in dynamodb aktualisiert.node.js übergibt einen Parameter an DynamoDB updateItem Methode

Zum Beispiel in einem DynamoDB Tabelle, wo jede userId der Schlüssel ich habe ist, Werte wie

{ "categoryname": "a", "skillState": "a", "skipcount": 1, "userId": "amzn1.ask.account.xxx” }

Ich will die "categoryname": "b" gesetzt, obwohl es 10 bis 15 Felder sein könnte wie folgt so dont Ich will hart Code der Feldname

function (userId,itemToUpdate,itemValue,callback) { 


    var updateExpressionString = "SET #"+itemToUpdate+" =:val1"; 
    var expressionAtt = '#'+itemToUpdate + ''; 

    console.log(updateExpressionString) 
    console.log(expressionAtt) 

    this.dynamodb.updateItem({ 
      TableName: constants.dynamoDBDetailTableName, 
      Key: { 
       userId: { 
        S: userId 
       } 
      }, 
      UpdateExpression: updateExpressionString, 
      ExpressionAttributeNames : { 
        expressionAtt : itemToUpdate 
      }, 
      ExpressionAttributeValues : { 
       ':val1': {'S':itemValue} 
      } 
     }, function (err, data) { 
      if (err) { 
       console.log(err) 
       console.log('Error ') 
      } else if (data.Item === undefined) { 

      }else { 
        console.log(data) 
      } 
    }); 
} 

In ExpressionAttributeNames:

{Validation: ExpressionAttributeNames enthält ungültigen Schlüssel: Syntaxfehler; Schlüssel: "expressionAtt"

Dies löst Fehler offensichtlich denken, dass expressionAtt der Schlüssel ist, während es eine lokale Variable ist.

ich node.js neu bin, wie kann die lokale Variable in zu ExpressionAttributeNames passieren und ExpressionAttributeValues

Antwort

1

Eine Möglichkeit des Umgangs mit dieser könnte das Objekt aus updateItem, steckte es in seine eigene Variable zu ziehen wie so:

var item = { 
    TableName: constants.dynamoDBDetailTableName, 
    Key: { 
     userId: { 
      S: userId 
     } 
    }, 
    UpdateExpression: updateExpressionString, 
    ExpressionAttributeNames: {}, 
    ExpressionAttributeValue: { 
     ':val1': {'S': itemValue} 
    } 
}; 

item.ExpressionAttributeNames[expressionAtt] = itemToUpdate; 

this.dynamodb.updateItem(item); 

glaube ich, dass Ihr Problem beheben

Verwandte Themen