[英]Angular Put Method not updating data
我是 MEAN 堆栈的新手,在使用 put 方法更新数据时遇到问题,我已经使用 postman 进行了测试,它工作正常,但是当我在 angular 上使用它时它不起作用。 没有出现任何错误,这是我在更新数据后在控制台中得到的 [is give success update][1] 但我更新的数据没有任何变化。 我对创建和删除方法没有问题,只是更新有问题的方法
这是我的代码
更新服务
updateData(id, data): Observable<any>{
let url = `${this.baseUri}/update/${id}`;
return this.http.put(url,data, { headers : this.headers }).pipe(
catchError(this.errorManagement)
)}
更新组件
OnSubmit(id){
let record = this.updateForm.value
if(!record){
this.notif.showError('can\'t do update data','Update data Error')
return false;
}else{
return this.motorService.updateData(id, record).subscribe(res=>{
console.log(record)
},(error) => {
console.log(error)
});
}}}
更新路线
listDataRoute.route('/update/:id').put((req,res,next)=>{
listData.findByIdAndUpdate(req.params.id,{
$set : req.body
},{new: true, useFindAndModify: false},(error, data)=>{
if (data.n > 0) {
res.status(200).json({
message: 'profile updated'
});
} else {
res.status(401).json({
message: 'not authorized'
});
}
})
.catch(error => {
res.status(500).json({
message: 'updating profile failed'
});
})
})
知道我做错了什么吗? 我已经被这个错误困住了 5 个小时,谢谢 [1]: https://i.stack.imgur.com/ryR8g.png
更新:在“更新路由”中添加一些代码后出现错误 401,仍然不知道如何解决此错误
我想分享我用平均堆栈更新数据的方式。 它与您的代码略有不同,但它可以工作并且经过了开玩笑的测试。 我想分享我更新用户资料的例子
我在我的后端 user.js 中创建了一个 userModel
// user.js
const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");
mongoose.set('useCreateIndex', true);
const userSchema = mongoose.Schema({
id: String,
email: { type: String, unique: true },
username: { type: String, unique: true },
password: { type: String, required: true },
currentLocation: String,
age: String,
});
userSchema.plugin(uniqueValidator);
module.exports = mongoose.model('User', userSchema);
然后我在 profileController.js 中编写更新方法
// profileController.js
exports.update = (req, res, next) => {
const user = new User({
_id: req.body.id,
email: req.body.email,
username: req.body.username,
currentLocation: req.body.currentLocation,
age: req.body.age,
});
User.updateOne({ _id: req.params.id }, user).then(result => {
if (result.n > 0) {
res.status(200).json({
message: 'profile updated'
});
} else {
res.status(401).json({
message: 'not authorized'
});
}
})
.catch(error => {
res.status(500).json({
message: 'updating profile failed'
});
});
}
在我的 profilerService.ts (前端)中,我还定义了与我的后端中的 user.js model 具有相同属性的用户类
// profile.service.ts
export class User {
id: string;
email: string;
username: string;
password: string;
currentLocation: string;
age: string;
constructor() {
this.id = '';
this.email = '';
this.username = '';
this.password = '';
this.currentLocation = '';
this.age = '';
}
}
我还将更新服务调用添加到我的服务文件中
// profile.service.ts
updateProfile(user: User, userId: string) {
return this.http.put(environment.backendUrl + '/api/user/' + userId, user);
}
然后我可以从我想更新我的用户的组件中调用服务 function
// profile-page.component.ts
updateProfile(user: User) {
this.profileService.updateProfile(user, user.id).subscribe(response => {
console.log('update successfull');
});
}
我希望我的代码片段能够帮助到你。 如果还有什么需要澄清的,请告诉我:对不起,如果这不是最完美的答案,因为这是我对 SO 的第一个答案之一 :)
401 表示身份验证错误。
发帖有效吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.