问题
I am working on nestjs and I want to retrieve data from DB on the basis of name property, which I am passing in a method of services code. The issue is when am passing {"name": "item1"} in postman, am getting item1 related data, but when am passing {"name":"item1","name":"item2"} in postman, I am getting data related to item2 only.I want to retrieve data of both item1, item2, and so on. To overcome this issue I thought rest parameters would be a good choice, but I don't know how to pass rest parameters and how to call rest parameters in postman. The main problem is I don't know how to deal with rest parameters in postman and code too.
Here is the code of services:
async individual(...name:any){
const ind= await this.usersmodel.find({'name':{$in:name}})
return ind
}
Here is the code of controller:
@Post('ind')
async ind(@Body('name')name){
return this.usersService.individual(name)
}
screenshot of postman:
screenshot of error am getting by using this method:
回答1:
Your question isn't well formulated but i will pretend you are trying to find in your schema a number of values that you will provide trought your API.
Thinking that way, you can use a POST to sendo an Array inside your body, like that:
"name": ["item2", "item3", "item4"]
Inside your service file you receive that array and can still use find(), but that way:
await this.usersmodel.find({
'name': { $in: name }
}
来源:https://stackoverflow.com/questions/62117712/during-a-function-call-how-can-i-use-rest-parameters-in-typescript