0

I am building an Angular app that uses a REST API built from NodeJS and LoopBack. When trying to create new records, I'm getting a weird error both from my app and also in Postman. (Property names have been changed to protect the innocent)

The `ObjectName` instance is not valid. 
Details: `id` can't be blank (value: undefined); 
`property2` can't be blank (value: undefined); 
`property3` can't be blank (value: undefined).

This is what I have posted:

{
    "id":0,
    "property2":"foo",
    "property3":"bar",
    "property4":"R"
}

The weirdest part of this is that when I try to run this through the LoopBack API Explorer, it works just fine. I have the same issues with PATCH methods as well.

Any thoughts?

Thanks

Chris Hampton
  • 109
  • 1
  • 9
  • how are sending data via post man can you post a screen shot or something – Rahul Singh Dec 02 '17 at 05:26
  • @RahulSingh I am sending it in the body tab, with the 'raw' option selected. That is how I have always sent json data with Postman. I cannot post a screenshot because I do not have a way to blur sensitive data. – Chris Hampton Dec 03 '17 at 19:40
  • @ChrisHampton: Did you set headers (`Content-Type`, `Accept`)? When you make a request via LoopBack API Explorer, it also generates a curl command. Does that curl command work for you? – Ivan Schwarz Dec 04 '17 at 10:37
  • @IvanSchwarz I've set the headers in Postman as well as in my angular app, but I get the same error. Incidentally, the curl command works just fine. Go figure... – Chris Hampton Dec 04 '17 at 14:42
  • @ChrisHampton: If you open network tab in chrome dev tools, is there any difference between the request made by API Explorer/curl and the request made by Postman/your app? – Ivan Schwarz Dec 04 '17 at 17:06
  • @IvanSchwarz No, there wasn't. However, I have found the cause. Please see my comment below. – Chris Hampton Dec 04 '17 at 20:27
  • @ChrisHampton Well, you just wrote in your answer, that the request is different. You were sending a different JSON object in your Angular app. Anyway, LoopBack shouldn't return this kind of Error if you add extra properties. I'm not able to reproduce that. I also still don't understand why postman didn't work but curl and API Explorer did for identical requests. I'm glad that you solved your issue though. – Ivan Schwarz Dec 04 '17 at 21:26

1 Answers1

0

I found the cause. In previous APIs I have worked with, the back-end was set up with a model binder that would apply the properties it needed and discard the ones it did not. So I have typically send the entire object in POSTs/PUTs/etc. In this case, we do not have that, so it was throwing this weird error. Once I added a method to my Angular auto-mapper that would strip the object down to only the bare necessities, it worked just fine.

In the end, I could have figured that out if the error message had been better.

Chris Hampton
  • 109
  • 1
  • 9