2017-03-10 1 views
2

ノードサーバーにstringの値を送信し、その値をMySQLテーブルで検索したいと考えています。Angular2からnode.jsにデータを送信する

私は、私はnodeサーバー

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

からデータをフェッチしているサービスをサービス

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

からデータを取得しているコンポーネントを持っているそして最後に、私はノードAPI

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

と私は

私はそれをどのように行うことができます

値は病気私のコンポーネントから送信することにより、その 「somekindofid」を置き換えたいですか?

答えて

1

idは、URL自体のノードメソッドに渡す必要があります。 同じ場合、APIルートを/profile/:idに変更する必要があります。ここで、idは、APIのコンシューマーから渡されるパラメーターです。

ノード

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); 
    } 
} 

サービス

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

コンポーネント

ngOnInit() { 
    let userId = 'pankaj'; 
    this.instructionsService.getAllInstructions(userId).subscribe(instructions => { 
     this.instructions = instructions; 
    }); 
} 
関連する問題