2017-12-18 253 views
1

我想通过服务进行更新。如果我使用与服务中链接相同的邮递员,更新可以正常工作。意味着更新不能从服务,只从邮递员

这里是我的代码:

myservice.service.ts:

updateUser(userid){ 
    let headers = new Headers(); 
    headers.append('Content-Type','application/json'); 
    console.log('http://localhost:3000/users/update/'+ userid); 
    return this.http.put('http://localhost:3000/users/update/'+ userid, 
{headers: headers}).map(res => res.json()); 
} 

内航线/ users.js

router.put('/update/:id', (req, res) => { 
User.findByIdAndUpdate(req.params.id, { 
$set: { 
    participates: true} 
}, 
{ 
    new: true 
}, 
function(err, updatedUser){ 
    if(err){ 
    res.send("Error updating user"); 
    } else{ 
    res.json(updatedUser); 
    } 
} 
); 
}); 

我没有得到一个错误,但它不似乎进入router.put。 我错过了什么?

+0

你有CORS标题吗? – Claies

+0

[How to allow CORS?](https://stackoverflow.com/questions/7067966/how-to-allow-cors) – georgeawg

+0

我使用'@ angular/http'中的import {Http,Headers};我使用const cors = require('cors'); app.use(cors());在我的index.js文件中 – Ivett

回答

0

这里你已经使用这个网址: 'http://localhost:3000/users/update/' +用户ID

但路线已使用:/更新/:身份证,

你有/用户是不是有你路线, 只需使用相同的URL,你的服务提到的将是很好的航线使用,所以用/用户/更新/更换路线您的网址:ID,那么它会很好地工作..