Angular with Express:为什么我的 PUT 请求没有触发?
Angular with Express: Why is my PUT request not triggering?
我的 GET 请求有效,但我的 PUT 请求似乎没有触发。它所在的函数肯定会运行,因为 console.log 语句显示在我的控制台中。 http PUT 请求中的 console.log 语句不会显示在 google chrome 控制台或我的终端中。
Angular:
getCitiesSaved(): Observable<string[]> {
return this.http.get(this.url).map((data: Response) => {
this.currentCityList = data.json().cityAlerts;
return data.json().cityAlerts;
});
}
addCity(cityName) {
let cityArr = this.currentCityList;
cityArr.push(cityName);
const body = JSON.stringify({
id: this.userId,
cityAlerts: cityArr
});
const headers = new Headers({ 'Content-Type': 'application/json' });
console.log(body);
return this.http
.put(this.url, body, { headers: headers })
.map((response: Response) => {
console.log('http put request ran in frontend');
response.json();
})
.catch((error: Response) => {
this.errorService.handleSigninError(error.json());
return Observable.throw(error.json());
});
}
快递:
router.get('/users/:id', (req, res) => {
console.log('get user info');
User.findOne({
_id: req.params.id
}).exec((err, user) => {
if (err) {
return res.status(500).json({
title: 'An error occured',
error: err
});
} else {
console.log(user);
res.status(200).json(user);
}
});
});
router.put('/:users/:id', (req, res) => {
console.log('backend triggered');
User.findOneAndUpdate(
{
_id: req.params.id
},
{ $set: { cityAlerts: req.body.cityAlerts } },
{ upsert: true },
function(err, newCityAlert) {
if (err) {
console.log(err);
} else {
console.log(newCityAlert);
res.send(newCityAlert);
}
}
);
});
您必须在请求执行之前对您的可观察对象调用 subscribe
。
看来您需要在 addCity()
.
内将 map
更改为 subscribe
来自文档:https://angular.io/guide/http
Note the subscribe() method. All Observables returned from HttpClient are cold, which is to say that they are blueprints for making requests. Nothing will happen until you call subscribe(), and every such call will make a separate request. For example, this code sends a POST request with the same data twice:
他们的例子:
const req = http.post('/api/items/add', body);
// 0 requests made - .subscribe() not called.
req.subscribe();
// 1 request made.
req.subscribe();
// 2 requests made.
希望对您有所帮助 - 我也无意中遇到了这个问题。
我的 GET 请求有效,但我的 PUT 请求似乎没有触发。它所在的函数肯定会运行,因为 console.log 语句显示在我的控制台中。 http PUT 请求中的 console.log 语句不会显示在 google chrome 控制台或我的终端中。
Angular:
getCitiesSaved(): Observable<string[]> {
return this.http.get(this.url).map((data: Response) => {
this.currentCityList = data.json().cityAlerts;
return data.json().cityAlerts;
});
}
addCity(cityName) {
let cityArr = this.currentCityList;
cityArr.push(cityName);
const body = JSON.stringify({
id: this.userId,
cityAlerts: cityArr
});
const headers = new Headers({ 'Content-Type': 'application/json' });
console.log(body);
return this.http
.put(this.url, body, { headers: headers })
.map((response: Response) => {
console.log('http put request ran in frontend');
response.json();
})
.catch((error: Response) => {
this.errorService.handleSigninError(error.json());
return Observable.throw(error.json());
});
}
快递:
router.get('/users/:id', (req, res) => {
console.log('get user info');
User.findOne({
_id: req.params.id
}).exec((err, user) => {
if (err) {
return res.status(500).json({
title: 'An error occured',
error: err
});
} else {
console.log(user);
res.status(200).json(user);
}
});
});
router.put('/:users/:id', (req, res) => {
console.log('backend triggered');
User.findOneAndUpdate(
{
_id: req.params.id
},
{ $set: { cityAlerts: req.body.cityAlerts } },
{ upsert: true },
function(err, newCityAlert) {
if (err) {
console.log(err);
} else {
console.log(newCityAlert);
res.send(newCityAlert);
}
}
);
});
您必须在请求执行之前对您的可观察对象调用 subscribe
。
看来您需要在 addCity()
.
map
更改为 subscribe
来自文档:https://angular.io/guide/http
Note the subscribe() method. All Observables returned from HttpClient are cold, which is to say that they are blueprints for making requests. Nothing will happen until you call subscribe(), and every such call will make a separate request. For example, this code sends a POST request with the same data twice:
他们的例子:
const req = http.post('/api/items/add', body);
// 0 requests made - .subscribe() not called.
req.subscribe();
// 1 request made.
req.subscribe();
// 2 requests made.
希望对您有所帮助 - 我也无意中遇到了这个问题。