首页 > 解决方案 > 节点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());

标签: node.jsangular

解决方案


在 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})'。


推荐阅读