1

I've searched the web. I found a couple of things but I wasn't able to understand anything out.

I'm trying to send a get request from my angular to Node.js server. I want to send some search parameters for the my server. and it searches then returns the documents.

I'm tring this function to get send the request. But I don't know how to ad parameters beside this request.

GetCases(){
        return this.http.get('/api/saleCases')
        .map((response:Response)=>response.json());
    }

In the server side

router.get('/saleCases', function(req, res){
    console.log('get request for all cases');
    Case.find({}).exec(function(err, cases){
        if(err){
            console.log("error retriving videos");
        }else{
            res.json(cases);
        }
    });
});

Can you help me to add the parameters in angular and read them in node.js? Or is there any kind of way that you can suggest me to use?

  • Possible duplicate of [Angular, Http GET with parameter?](https://stackoverflow.com/questions/44280303/angular-http-get-with-parameter) – Salim Ibrohimi Sep 26 '17 at 13:42

1 Answers1

2

In Client side, first init your search parameters, something like:

const searchParams = {
    params: {
        param1: val1,
        param2: val2,
        ...
    }
}

then send your request to Server side like:

return this.http.get('/api/saleCases', searchParams)

In Server side you can access the params, using:

router.get('/saleCases', function(req, res){
    const param1 = req.query.param1;
    const param2 = req.query.param2;
});
0Be95
  • 36
  • 4
  • It errors on searchParams. it says: [ts] Type '{ param1: number; param2: number; }' has no properties in common with type 'RequestOptionsArgs'. – Ashkan R. Nejad Sep 26 '17 at 13:45
  • I believe it expects a params property (https://angular.io/api/http/RequestOptionsArgs). please try: const searchParams = { params: { param1: val1, param2: val2, } }, – 0Be95 Sep 26 '17 at 14:06