0

I have a Deployd API which exposes a json structure like this:

[
  {id: "1"
   username: "john",
   password: " ..... ",
   email: "example@gmail.com",
   coins: 60,
   badges: [ ],
   courses:[
       { id: "123456",
         title: "Animals",
         grades_per_module: [ [30], [28, 26], [25, 24]]
         ..... 
       },
       { id: "112233",
         title: "Food",
         grades_per_module: [ [20, 25, 27], [22]]
         ..... 
       }
    ]
  }, 

  {id: "2"
   username: "mark",
   password: " ..... ",
   email: "ex@gmail.com",
   coins: 40,
   badges: [ ],
   courses:[
       { id: "123456",
         title: "Animals",
         grades_per_module: [ [27], [21, 30], [30, 30]]
         ..... 
       }
    ]
  }
] 

Then I need to remove the intere course with id="112233" of the user "john" using an angular code.

So I use this code, but it doesn't work:

this.http.put('http://localhost:2403/users/1',
              { "courses": { $pull: { "id": 112233 } }
             }).subscribe( ..... )

Deployd API returns me a positive message, but the course is not really removed from the user object. Can anyone help me?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
FedeLanza
  • 173
  • 1
  • 3
  • 15
  • 1
    Your question is actually more about the deployd implementation but you are not actually showing that code in the question. You need to instead alter the question content to show that code. Also, does not matter that a request is coming from Angular other than it's a PUT where the API expects a PUT and any expected request BODY. However I **strongly discourage** API's exposing database internal syntax. If anything the request path should be something like `/users/:id/removeCourse/:courseId` Where the update operation is fixed in the method and the parameters come from the URI – Neil Lunn Apr 19 '19 at 22:11
  • 1
    That said the "positive message" is probably indicating that either the document was not matched or not modified, and these are actually indicators that the parameters are wrong. In fact your `id: "123456"` is a "string" and you appear to be sending content that is likely being interpreted as `numeric`. – Neil Lunn Apr 19 '19 at 22:16
  • Thank you for the advices. To be honest, until now I have always tried to manipulate database internals directly with the http Angular requests, as I have shown before. But as you said, this is not a good practice. I don’t know how to manipulate the content of the database through Deployd functions and passing the parameters of the functions in the URI, as you told me before. – FedeLanza Apr 20 '19 at 09:25
  • With respect to the Json structure that I have shown before, may you show me an example of this code in which I can remove from the database an entire course of a specific user or modify only some internal properties through a Deployd function and passing the parameters in the URI? Thank you very much. – FedeLanza Apr 20 '19 at 09:25

0 Answers0