0

I'm trying to implement a PUT (update) API endpoint, passing the primary key of an element as a parameter in the path and the attributes I want to update as parameter in the body. But is not working as expected, instead of updating the attributes of an existing element, is creating a new element in the database with wrong attributes.

As far as I understand from the Dynamoose API documentation, Model.update(key, update, options, callback) updates and existing item in the table. For example, if we have a Dog model where age is one of the attributes, then this code

Dog.update({ownerId: 4, name: 'Odie'}, {age: 1}, function (err) {
  if(err) { return console.log(err); }
  console.log('Just a puppy');
})

would update the age of a Dog called 'Odie' with ownerId: 4

Now, I tried to do a similar update for my own API. I have a data model called InvoiceConfig where the primary key corresponds to a unique name/id (string) attribute and contains another attribute called providerVariants (an array of objects)

This is my API definition in swagger:

put:
  description: Updates the invoice config matching the id (providerName)
  operationId: updateInvoiceConfigByName
  consumes:
    - application/json
  produces:
    - application/json
  parameters:
    - name: id
      in: path
      required: true
      type: string
    - name: providerVariants
      description: array of JSON objects
      in: body
      required: true
      schema:
        $ref: "#/definitions/ProviderVariantsDataList"
  responses:
    "200":
      description: A confirmation message
      schema:
        $ref: "#/definitions/ResponseMessage"
    "500":
      description: Error message
      schema:
        $ref: "#/definitions/ErrorResponse"

And this is the function that implements the dynamoose update in my code:

updateInvoiceConfigByName: function(req, res) {
   var name = req.swagger.params.id.value;
   var providerVariants = req.body;
   console.log("UPDATE /invoice-config");

   InvoiceConfig.update({provideName: name}, providerVariants, function(err) {
      if (err) {
         console.log("Update InvoiceConfig error");
         throw err;
      }

      res.status(200).send({ 
         providerName: `${name}`,
         message: 'provider invoice cfg updated'
      });
   });
}

I have an object in the database:

{
    "providerName": "ID01",
    "providerVariants": [
      {
        "displayName": "My 1st Provider ",
        "phone": "915698471",
        "availableTemplates": [
          "default"
        ]
      }
    ]
}

and I try to update it from swagger-ui passing the following paramenters: ID01 in the endpoint path itself and a modified providerVariants array in the body:

[
   {
      "displayName": "My new name",
      "phone": "913333333",
      "availableTemplates": [
         "default"
      ]
   }
]

But as I said at the beginning, if I check the contents of my table, I see that the item with providerName "ID01" has not changed, and there is a new item created:

{
    "providerName": {
       "S": "[object Object]"
    }
 }

I suspect that in this new object the providerName (primary key) was populated with the providerVariants array, which is totally wrong. Any hints about how to fix this are welcome, I don't know how to proceed with the update. Other endpoints (get, delete, post) in my API are working fine, but I'm blocked with the update/put

rodrunner
  • 1,860
  • 4
  • 23
  • 34

1 Answers1

1

There is a typo in your update.

InvoiceConfig.update({provideName: name}, providerVariants, function(err) 

You're missing the 'r' in providerName :)