During a function call how can I use rest parameters in Typescript?

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-17 13:08:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!