2017-12-18 255 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,那麼它會很好地工作..