error 500 when using an array in post method.
Hi guys Back to the issue why Loopback + MongoDB is not working? another problem I found:
I removed exclude from schema:
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Use_r, { exclude: ['id'] }_),
},
},
})
user: Omit<User, 'id'>,
): Promise<User> {
return this.userRepository.create(user);
}
then I add generated: true and removed required: true from the user model .
@property({
type: 'number',
id: true,
generated: true
})
id: number;
it is looking fine when I used POST method, for one record/document when in add several records it ( array of records )I get:
[{
"email": "[email protected]",
"password": "571087741"
},
{
"email": "[email protected]",
"password": "346470131"
},
{
"email": "[email protected]",
"password": "243339637"
}]
{
"error": {
"statusCode": 500,
"message": "Internal Server Error"
}
}
Unhandled error in POST /users: 500 TypeError: model.toObject is not a function
at UserRepository.toEntity (D:\apps\test\node_modules\@loopback\repository\src\repositories\legacy-juggler-bridge.ts:471:39)
at UserRepository.create (D:\apps\test\node_modules\@loopback\repository\src\repositories\legacy-juggler-bridge.ts:338:17)
But regardless of the error message, it was POSTed all records to MongoDB correctly data with autogenerated id.
Acceptance criteria
- Improve our REST validation to reject Array values for parameters accepting an object - see #4701
- Modify
DefaultCrudRepository.createto reject requests to create multiple model instances in a single call
🎆 Hacktoberfest 2020
Greetings :wave: to all Hacktoberfest 2020 participants!
Here are few tips 👀 to make your start easier, see also #6456:
- Before you start working on this issue, please leave a comment to let others know.
- This issue consists of several tasks to work on, it may feel like a too big effort to undertake. Don't worry! It's perfectly fine to pick just one item from the list and leave the rest for somebody else. In fact, we prefer to have a dedicated pull request for each part, to make it easier for us to review the changes and get the pull request landed faster. Baby steps FTW! Remember, every little helps.
- If you are new to GitHub pull requests, then you can learn about the process in Submitting a pull request to LoopBack 4.
- If this is your first contribution to LoopBack, then please take a look at our Developer guide
- Feel free to ask for help in
#loopback-contributorschannel, you can join our Slack workspace here.
Can't help without seeing the code, post an example repo. This is not clear at all.
Can't help without seeing the code, post an example repo. This is not clear at all.
please find the project here: https://github.com/rsa408/lb4-test also, test data.json is there.
First thing, if you are generating id then you should exclude the id on creates request body. What you have currently didn't make sense by allowing users to be submitted with id while generating is enabled. Also can you confirm that you've used npm run migrate to set up the db correctly?
When you have more than one value, are you submitting them one at a time or trying to send an array of the items in a batch?you'll need to change request body to accept an array of it it's the later.
Hi @dougal83 when I exclude the id on creates request body with generated id, I receive 422 error:
{
"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": "not",
"message": "should NOT be valid",
"info": {}
}
]
}
}
I used npm run migrate but nothing changed in result getting error 422 with exclude id without inserting data into DB, and error 500 without exclude id with inserted data into DB.
For one value there is no issue (without exclude id) but for the array of values I got same as above error 500 .
how to change the request body to accept both an array of values and single value?
thanks.
What is the request body you are submitting? I'll see if I can replicate error on my end.
What is the request body you are submitting? I'll see if I can replicate the error on my end.
If I guess correctly is : (low knowledge)
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(User),
},
},
})
user: Omit<User, 'id'>,
): Promise<User> {
return this.userRepository.create(user);
}
from user controller
Sorry, I was not clear enough. What are you submitting to the end point that gives an error. If it is the entirety of your data json file in one go then I would say that is probably the issue? Please confirm the data you submit then I'll load up your code to see error.
EDIT: An example of posting an array of data: https://github.com/strongloop/loopback-next/issues/2820 Just incase that is what you're up to... considering the first post about the same issue has the pertinent advice. Other general advice is to ensure you're working with a clean db as it can sometimes cause issues with dirty data. Have fun.
I was able to reproduce the error using https://github.com/rsa408/lb4-test and posting the following request to http://localhost:3000/users via REST API Explorer:
[{
"email": "[email protected]",
"password": "571087741"
},
{
"email": "[email protected]",
"password": "346470131"
},
{
"email": "[email protected]",
"password": "243339637"
}]
Observed behavior:
- New model instances are created in the database
- The code processing result of the database operation throws an error and thus triggers 500 response.
What I'd like us to change:
- Improve our REST validation to reject Array values for parameters accepting an object.
- Modify
DefaultCrudRepository.createto reject requests to create multiple model instances in a single call
Same issue here.
I am afraid we don't have bandwidth to address this ourselves, we are looking to our community to step up and contribute a fix as outlined in https://github.com/strongloop/loopback-next/issues/3700#issuecomment-538857137.
See our Contributing guide and Submitting a pull request to LoopBack 4 to get started.
Let me know if you need any help along the way.
I am afraid we don't have bandwidth to address this ourselves, we are looking to our community to step up and contribute a fix as outlined in #3700 (comment).
See our Contributing guide and Submitting a pull request to LoopBack 4 to get started.
Let me know if you need any help along the way.
Hello @bajtos can I start working on this issue?
@Sharonee sure, let me assign it to you.
@Sharonee sure, let me assign it to you.
Hi @bajtos I have found that fix should be in @loopback/repository under "create" function, how can I build a new module for loopback/repository? do we have any documentation for that?
thx Sharon
I have found that fix should be in @loopback/repository under "create" function,
Yes please!
how can I build a new module for loopback/repository? do we have any documentation for that?
@Sharonee I am afraid I don't understand. What kind of a module would you like to build? It should be enough to modify the existing code, see our Contributing guide and Submitting a pull request to LoopBack 4 for more details.
Same problem with object works but no with an array: My controller:
@post('/usuarios-grupos', {
responses: {
'200': {
description: 'UsuariosGrupos model instance',
content: { 'application/json': { schema: getModelSchemaRef(UsuariosGrupos) } },
},
},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(UsuariosGrupos, {
title: 'NewUsuariosGrupos',
exclude: ['id'],
}),
},
},
})
usuariosGrupos: UsuariosGrupos | UsuariosGrupos[],
): Promise<UsuariosGrupos | UsuariosGrupos[]> {
if (_.isArray(usuariosGrupos)) {
return this.usuariosGruposRepository.createAll(usuariosGrupos);
} else {
return this.usuariosGruposRepository.create(usuariosGrupos);
}
}
@bajtos : Can I start working on this issue?
This error is no longer reproducible.
- Updated project dependencies
- Tried POST on /users with payload
[{
"email": "[email protected]",
"password": "571087741"
},
{
"email": "[email protected]",
"password": "346470131"
},
{
"email": "[email protected]",
"password": "243339637"
}]
- I now get an expected error
{
"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": "type",
"message": "must be object",
"info": {
"type": "object"
}
}
]
}
}
I will be reproducing this issue to determine whether problem still exist.