2

So i wanna send some string value to my node server and find that value in MySQL table.

I have a component which is getting data from service

ngOnInit() {
    this.instructionsService.getAllInstructions().subscribe(instructions => {
      this.instructions = instructions;
    });
}

Then i have a service which is fetching data from node server

getAllInstructions() {
    return this.http.get('/api/profile/')
      .map(res => res.json());
}

And finally i have node api

app.get('/profile',getAllInstructions);
function getAllInstructions(req,res){
connection.query("select * from users where id='somekindofid'",function(err, rows, fields) {
        res.json(rows);
    }
}

`

And i wanna replace that 'somekindofid' by the value ill be sending from my component

How can i do that?

1 Answer 1

1

You should be passing id to node method inside its URL itself. For the same you should change API route to /profile/:id where id is parameter that will be passed from consumer of the API.

Node

app.get('/profile/:id',getAllInstructions);
function getAllInstructions(req,res){
   connection.query("select * from users where id="+ req.params.id,function(err, rows, fields) {
        res.json(rows);
    }
}

Service

getAllInstructions(id) {
    return this.http.get(`/api/profile/${id}`)
      .map(res => res.json());
}

Component

ngOnInit() {
    let userId = 'pankaj';
    this.instructionsService.getAllInstructions(userId).subscribe(instructions => {
      this.instructions = instructions;
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.