React fetch('http:// localhost:3000 / profile /:id')

时间:2018-10-18 19:15:35

标签: javascript node.js reactjs express

如标题所示,我正在尝试从节点/快递服务器获取此URL。该路由在Postman中运行良好,因此我相信它告诉我错误是在获取方法中。

我的代码:

onSubmitSave = () => {
    fetch('http://localhost:3000/profile/:id', {
        method: 'put',
        headers: {'Content-Type': 'application/json' },
        body: JSON.stringify({
            name: this.state.name, 
            telephone: this.state.telephone,
        })
    })
    .then(response =>  response.json())
    .then(user => {
        console.log(user);
    }).catch(err => console.log(err))

} 

如果有人可以帮助我,我会很高兴。

1 个答案:

答案 0 :(得分:4)

我看不到您的:id被任何字符串插值代替。您可能正在寻找这样的东西:

fetch(`http://localhost:3000/profile/${this.id}`)