5
const AWS = require('aws-sdk')
const docClient = new AWS.DynamoDB.DocumentClient({ region: 'eu-central-1' })

const createDocument = (text, callback) => {
  const createParams = {
    Item: {
      text: text
    },
    TableName: 'ToDoItems'
  }

  docClient.put(createParams, (err, data) => {
    if(err) {
      callback(err, null)
    } else {
      callback(null, data)
    }
  })
}

exports.handle = (event, context, callback) => {
  createDocument(event.text, (err, data) => {
    if(err) {
      callback(err, null)
    } else {
      callback(null, data)
    }
  })
}

That's my AWS Lambda function, the issue is that when I get a callback, data object is empty, even though document is inserted into DynamoDB. What could the issue be here?

4 Answers 4

9

You can't. You have to separately query. On put, if you set ReturnValues: 'ALL_NEW', then you'll get "ReturnValues can only be ALL_OLD or NONE"

Note the 'possible' in AWS's documentation:

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property

ReturnValues — (String) Possible values include:

  • "NONE"
  • "ALL_OLD"
  • "UPDATED_OLD"
  • "ALL_NEW"
  • "UPDATED_NEW"

Also, instead of separately querying, you can also just use the params value. If it was saved, then what you have in createParams.Item is basically what's returned if you were to separately query.

Sign up to request clarification or add additional context in comments.

2 Comments

that's what I ended up doing.
Also, instead of separately querying, you can also just use the params value. If it was saved, then what you have in createParams.Item is basically what's returned if you were to separately query.
4

There is a workaround - You can use update method of DynamoDB.DocumentClient.

    TableName: "table",
    Key: {
        id: randomId
    },
    AttributeUpdates: {
        authorId: {Action: "PUT", Value: event.authorId},
        date: {Action: "PUT", Value: event.date},
        description: {Action: "PUT", Value: event.description},
        title: {Action: "PUT", Value: event.title}
    },
    ReturnValues: "ALL_NEW"

This method will create new item and return all what you need

1 Comment

It did not work Says : The number of conditions on the keys is invalid
1

You have to request the return values, like this:

  const createParams = {
    Item: {
      text: text
    },
    TableName: 'ToDoItems',
    ReturnValues: 'ALL_NEW'
  }

This is documented here.

3 Comments

Thanks for hint, I missed that. I get ReturnValues can only be ALL_OLD or NONE error though. Any idea why?
I see, docs.aws.amazon.com/amazondynamodb/latest/APIReference/… says put only accepts ALL_OLD or NONE
Ah right, you are doing a put, not an update, so those are the only options. Basically it doesn't want to expend the bandwidth returning the item to you when you already have the item in a variable in your script. It will let you do ALL_OLD if you want to know what the old value was (in the case that put_item replaced an existing item).
0

I did have to implementing that the return in .then() was params.Item, like this:

var params = {
        TableName:table,
        Item:{

            "name": value,
            "email": value2,
        }
    };

    console.info("Adding a new item...");
    await docClient.put(params)
    .promise()
    .then(data => {
        return params.Item;
    }).catch(error => {
        console.error(error)
        throw new Error(error)
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.