node.js - 节点js的后角形式参数错误
问题描述
我是角度和节点js的新手我有一个角度的多选项卡表单,在一个选项卡中我想以html表单将带有节点js的记录插入mysql表我有这个:
<mat-card-content fxLayout="column" [formGroup]="formGroupCaption" >
<mat-form-field>
<input matInput placeholder="caption" formControlName="caption" autocomplete="off">
<span *ngIf="!formGroupCaption.get('caption').valid && formGroupCaption.get('caption').touched">insert caption</span>
</mat-form-field>
</mat-card-content>
<mat-card-actions >
<button mat-raised-button color="primary" type="submit" (click)="insert_caption()">Login</button>
</mat-card-actions>
在 ts 文件中,我有以下内容:
ngOnInit(): void {
this.get_data();
this.Create_Form_Caption();
}
Create_Form_Caption(){
this.formGroupCaption=new FormGroup({
'caption':new FormControl(null,[Validators.required])
})
}
insert_caption(){
const caption=this.formGroupCaption.value.caption;
console.log(caption);
this.http.post("http://localhost:3000/item_create",caption).subscribe(
(res) => {console.log(res)}
);
我的js文件是:
app.post("/item_create",(req,res)=>{
console.log('caption is :' + req.body.params.caption);
const insertquery="INSERT INTO captiontbl (description) VALUES ('" + req.body.params.caption + "')";
ConnectToDB().query(insertquery,(err,result)=>{
if (err){
console.log("error"+err);
res.sendStatus(500);
return;
}
res.send("inserted" + result);
res.end();
})
我在nodejs中收到错误: **无法读取未定义的属性“标题” 并且不返回标题的值
添加 body_parser 定义有按摩:'bodyParser' 已弃用。和 bodyParser 用删除线显示:
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
解决方案
在 api 调用中使用 'req.body.caption' 而不是 'req.body.params.caption' 并检查。在 angular end 将 'this.http.post("localhost:3000/item_create",caption)' 更改为 'this.http.post("localhost:3000/item_create",{caption})'。
推荐阅读
- ruby - 如何在 IRB 中要求 ActiveRecord
- python - 如何使用 gensim 工具包找到一个词或多词释义的同义词
- python-3.x - 使用 pandas 加载 CSV 文件时如何解决 keyerror
- yaml - VSTS - Android 签名 - 安全文件不存在或未被授权使用
- c - 减小指针直到它指向数组的第一个元素
- python - 熊猫在使用带有 header=None 的 read_csv() 时给出不寻常的结果
- java - 为用户生成一个 RANDOM id
- ruby - Ruby hash:返回第一个不为 nil 的键值
- c - scanf 无法读取 C 中的字符串
- django - Django zappa 403 禁止错误和 net:: Err_Aborted