[英](MEAN STACK) nothing happens when i use put method to update a document
i have an application that's supposed to do CRUD, very simple.我有一个应该做 CRUD 的应用程序,非常简单。 i want to update a book's info but it doesn't work so what should i change?
我想更新一本书的信息,但它不起作用所以我应该改变什么? plus i'm getting this error now:
另外我现在收到此错误:
core.js:6210 ERROR TypeError: Cannot read properties of undefined (reading 'value') core.js:6210 错误类型错误:无法读取未定义的属性(读取“值”)
backend code:后端代码:
//EDIT ONE BOOK BY NAME
booksRoute.route('/edit/:name').put((req,res,next)=>{
BookModel.findOneAndUpdate(req.params.name,
{$set:false},
(error,data)=>{
if (error){
console.log(error);
return next(error);
}else{
res.json(data) ;
console.log('data updated successfully');
}
})
})
FRONTEND CODE:前端代码:
<div id="bg"><div class="addme">
<ul>
<form #f="ngForm" (ngSubmit)="onSubmit(f)" ngNativeValidate>
<li>
<label>book's name *</label>
<input name="name" type="text" required>
</li>
<li>
<label>book's genre *</label>
<input name="genre" type="text" ngModel required>
</li>
<li>
<label>book's author *</label>
<input name="author" type="text" ngModel required>
</li>
<li>
<label>rating *</label>
<input name="rating" type="number" [max]="10" min="0" ngModel required>
</li>
<li>
<label>price *</label>
<input name="price" type="number" ngModel required>
<input class="btn btn-primary" type="submit"/>
</li>
</form>
</ul>
</div>
</div>
service服务
//edit one book by it's name
editBook(name:string, data){
return this.http.put(this.backendUrl +'/edit/'+name,data) ;
}
edit-component.ts编辑组件.ts
onSubmit(f){
console.log("name value:" + f.name.value)
this.apiService.editBook(f.name.value,f.value).subscribe( res => {console.log('backend\' response:',res)}) ;
} }
so the problem is you are not specifying a field to update do something like this:所以问题是您没有指定要更新的字段,请执行以下操作:
booksRoute.route('/edit/:name').put((req,res,next)=>{
BookModel.findOneAndUpdate(req.params.name,
{$set: {
fieldname: false
}},
(error,data)=>{
if (error){
console.log(error);
return next(error);
}else{
res.json(data) ;
console.log('data updated successfully');
}
})
})
finally i found the solution after reading documentations.最后我在阅读文档后找到了解决方案。 i have two obvious mistakes in my code, the first one is that i didn't assign my input field that i'm gonna use to filter the update request to [(ngModel)] ('name').
我的代码中有两个明显的错误,第一个是我没有将用于过滤更新请求的输入字段分配给 [(ngModel)] ('name')。 so it's like this in my template :
所以在我的模板中是这样的:
<li>
<label>book's name *</label>
<input name="name" type="text" [(ngModel)]="f.name" required>
</li>
the next mistake is that findOneAndUpdate method is missing the request body so it should be like this to work:下一个错误是 findOneAndUpdate 方法缺少请求正文,所以它应该像这样工作:
//EDIT ONE BOOK BY NAME
booksRoute.route('/edit/:name').put((req,res,next)=>{
BookModel.findOneAndUpdate(req.params.name,req.body,
{new:true},
(error,data)=>{
if (error){
console.log(error);
return next(error);
}else{
console.log(data)
res.json(data) ;
console.log('data updated successfully');
}
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.