sourcefuse / loopback4-starter

Loopback 4 starter application. Multi-tenant architecture supported. Authentication, Authorization, Soft deletes, environment vars, Audit logs, included.
MIT License
158 stars 59 forks source link

Getting 422, property required although I am passing it in the request body?! #22

Closed sherif2011 closed 5 years ago

sherif2011 commented 5 years ago

Why when I call a post request for groups controller (customized), I am getting 422 although I am passing property name??

code here

image

Please help, and give it a high priority. I like loopback, but then I feel it's not reliable yet?

sherif2011 commented 5 years ago

Please help on this one ASAP, I am half tempted switching to another framework :(

samarpan-b commented 5 years ago

Yes @sherif2011 . We assure you that we will help you at our earliest. It seems that you are having a lot of understanding gaps with LoopBack. Please go through the LoopBack documentation carefully so that you don't get entangled into such gaps. Also, in this case, it seems you were just using an incorrect custom query (copy-paste error, it seems). @mayank-SFIN571 will explain more.

mayank-SFIN571 commented 5 years ago

@sherif2011 you need to update the custom query written in group.repository.ts

the query is present in create function, and it goes like this INSERT INTO admin.roles( clients, name, deleted) VALUES ('${clients}', '${entity.name}', '${entity.deleted}')

The problem here is we are targeting the wrong table in database i.e roles. We need to make an entry in groups. Also we are not sending deleted boolean which is setting the value to be undefined. so we need to make following changes to the query.

INSERT INTO admin.roles( clients, name) VALUES ('${clients}', '${entity.name}')

Hope this helps, if the problems still persists, do inform.

samarpan-b commented 5 years ago

Just a note, LoopBack v4 is a great framework. You just need to give it thorough understanding time initially. Once, you get a hold of it, you will be much quicker in building APIs than many others. We ourselves have used LoopBack 4 in many of our production ready applications. All have been pretty successful and very quick to develop.

sherif2011 commented 5 years ago

Hey @mayank-SFIN571 I did the fixes yesterday but maybe didn't push to github. Unfortunately, that didn't work. I even added a console.log first thing in controller, and it doesn't seem to be hitting it:

@authorize(['*']) @post('/groups', { responses: { '200': { description: 'Group model instance', content: {'application/json': {schema: {'x-ts-type': Group}}}, }, }, }) async create(@requestBody() group: Group): Promise { console.log('i am here!'); return await this.groupRepository.create(group); }

And this is my request:

POST http://localhost:3000/groups { "name":"test", "clients":["amac","fdfl"] }

it's weird that I am passing the name but am still getting:

{"error":{"statusCode":422,"name":"UnprocessableEntityError","message":"The request body is invalid. See error object details property for more info.","code":"VALIDATION_FAILED","details":[{"path":"","code":"required","message":"should have required property 'name'","info":{"missingProperty":"name"}}]}}

Thanks for advising!

sherif2011 commented 5 years ago

I pushed to github, please check here Thanks

mayank-SFIN571 commented 5 years ago

@sherif2011 I tried creating a group and I did that without any issues.

Delete the dist folder, create a new build and then try the same. I think that should resolve the issue. Build making process has a bug, and will be resolved from loopback team soon.

sherif2011 commented 5 years ago

Thanks @mayank-SFIN571 , that fixed it.

sherif2011 commented 5 years ago

By the way, the solution mentioned in this ticket fixed it for me. Thanks!

samarpanB commented 5 years ago

Thanks for the update. Yes, that was the issue raised by Mayank on LoopBack repo. Thankfully it got resolved soon

sherif2011 commented 5 years ago

I am reopening this one. It's happening again. I am getting a 422 ":"VALIDATION_FAILED","details":[{"path":"","code":"required","message":"should have required property 'name'","info":{"missingProperty":"name"}} when I make a put request to http://localhost:3000/groups/2

Here is code base (under dev branch)

Also you can npm test to see how I am calling the API. Btw I tried both passing and not passing id in request body.

I also made sure to rebuild, by deleting dist, changing dist in packagejson's command: "build": "lb-tsc --outDir dist11 --copy-resources", then renaming dist11 to dist. I am sure the build is still not working properly though!

Please advise ASAP as I am passed my due date for this project.

Thanks!!

mayank-SFIN571 commented 5 years ago

Hello @sherif2011 I can't find any post route with url /groups/{id} . Can you please mention which file is giving you error.

Also can you share the exact trace of this error.

sherif2011 commented 5 years ago

Sorry, I meant to say put request:

it('put group by id', async () => { await client .put('/groups/2') .set('Accept', 'application/json') .set('Authorization', 'Bearer ' + tokenDetails.accessToken) .send(groupDetails) .expect(204); });

Group Controller put group by id: Error: expected 204 "No Content", got 403 "Forbidden" at Test._assertStatus (node_modules\supertest\lib\test.js:268:12) at Test._assertFunction (node_modules\supertest\lib\test.js:283:11) at Test.assert (node_modules\supertest\lib\test.js:173:18) at localAssert (node_modules\supertest\lib\test.js:131:12) at C:\Users\smankarious\Desktop\admin\node_modules\supertest\lib\test.js:128:5 at Test.Request.callback (node_modules\superagent\lib\node\index.js:728:3) at parser (node_modules\superagent\lib\node\index.js:916:18) at IncomingMessage.res.on (node_modules\superagent\lib\node\parsers\json.js:19:7) at endReadableNT (_stream_readable.js:1129:12) at process._tickCallback (internal/process/next_tick.js:63:19)

sherif2011 commented 5 years ago

My bad. Please ignore this issue. Seems to be an authorization issue.