Home > Mobile >  Cannot overwrite DynamoDB M values
Cannot overwrite DynamoDB M values

Time:01-07

I would like to delete information from a DynamoDB item.

To do so, I update a field to overwrite it with ''. This works for all the other attributes I have for the item, just not M values. I can neither update it to '', M: '', nor M: {S:''}. Instead I get a generic RequestAbortedError: Request aborted.

I am pretty confident this boils down to ignorance on my part. I thank you all for you time to help me.

Here is a sample of the code I am using:

function checkInputs(event) {
    if (event) {
        return event.value;
    } else {
        return ''
    }
}

export const EditRecipe = () => {

    function HandleSubmit(event) {

    // Load the AWS SDK for Node.js
    var AWS = require('aws-sdk');

    // Create DynamoDB service object
    var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

        // Capture Values
        var nvNodes = event.target.nutrientsValue
        if (nvNodes.length > 1){
            var currentNutrientsValue = Array.prototype.map.call(nvNodes, function(t) { return {S : t.value}; });
        } else {
            var currentNutrientsValue = [{S : checkInputs(nvNodes)}];
        }
         
        // Capture Types
        var ntNodes = event.target.nutrientsType
        if (ntNodes.length > 1){
            var currentNutrientsType = Array.prototype.map.call(ntNodes, function(t) { return {S : t.value}; });
        } else {
            var currentNutrientsType = [checkInputs(ntNodes)];
        }

        //zip types to values
        var nutrientsMap = {};
        currentNutrientsType.forEach((key, i) => nutrientsMap[key] = currentNutrientsValue[i]);

        // Call DynamoDB to add the item to the table
        var updateParams = {
            TableName: 'recipes',
            Key: {'recipe_index': data},
            UpdateExpression: 'nutrients = :nutrients',
            ExpressionAttributeValues: {
                ':nutrients': {M: nutrientsMap}
            },
        };

        ddb.updateItem(updateParams, function(err, data) {
            if (err) {
                console.log("Error", err);
            } else {
                console.log("Success", data);
            }
        });


    return (<form onSubmit={HandleSubmit} method="post">
        <Nutrients tableName='recipes' title={title} edit={true}/>
        <input value='Save' type="submit"/> 
    </form>);

CodePudding user response:

You initial issue is that you are not using SET in your UpdateExpression. It should look like below:

var updateParams = {
    TableName: 'recipes',
    Key: {'recipe_index': data},
    UpdateExpression: 'SET nutrients = :nutrients',
    ExpressionAttributeValues: {
        ':nutrients': {M: nutrientsMap}
    },
};

DynamoDB is schemaless, so its best not to add empty values for attributes, but rather remove them by calling REMOVE.

var updateParams = {
    TableName: 'recipes',
    Key: {'recipe_index': data},
    UpdateExpression: 'REMOVE myattribute',
};
  • Related