loopbackio / loopbackio/loopback-next
error 500 when using an array in post method.
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1.1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 27
Description
Hi guys
Back to the issue [why Loopback + MongoDB is not working?](https://github.com/strongloop/loopback-next/issues/3645) another problem I found:
I removed exclude from schema:
```ts
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Use_r, { exclude: ['id'] }_),
},
},
})
user: Omit,
): Promise {
return this.userRepository.create(user);
}
```
then I add `generated: true` and removed `required: true` from the user model .
```ts
@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:
```json
[{
"email": "nunc.sed@semelit.net",
"password": "571087741"
},
{
"email": "lacus@dui.org",
"password": "346470131"
},
{
"email": "ipsum@aaliquetvel.org",
"password": "243339637"
}]
```
```json
{
"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.create` to 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](https://loopback.io/doc/en/lb4/submitting_a_pr.html).
- If this is your first contribution to LoopBack, then please take a look at our [Developer guide](https://loopback.io/doc/en/lb4/code-contrib-lb4.html)
- Feel free to ask for help in `#loopback-contributors` channel, you can join our Slack workspace [here](https://join.slack.com/t/loopbackio/shared_invite/zt-8lbow73r-SKAKz61Vdao~_rGf91pcsw).
Contributor guide
Assessment
This issue has not been assessed yet.