【问题标题】:Express Cannot read property 'name' of undefined from backExpress 无法从后面读取未定义的属性“名称”
【发布时间】:2020-04-16 12:44:55
【问题描述】:

我尝试使用 node js 和 angular (material ui) 开发一个全栈应用程序。

我阻止了以下问题的问题。

我正在制作一个小型网络资源管理应用程序。我目前处于Mysql数据库中数据的注册和发送阶段。

我收到以下错误:

TypeError: 无法读取未定义的属性“名称”

这是我的代码:

const express = require('express');
const app = express();
var cors = require('cors');
var mysql      = require('mysql');
const body = require("body-parser");

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '',
  database : 'totale',
});

app.use(cors());

app.get('/', function (req, res) {
  res.send('Hello World!');
  console.log(req);
});

app.get("/inscription",(req,res)=>{
  var nom = req.body.name;
  var prenom = req.body.prenom;
  var mail = req.body.email;
  var password = req.body.password;
  connection.connect();
  connection.query('INNSERT INTO inscription set nom = ?, prenom = ? ,mail = ? ,password = ? ',[nom,prenom,mail,password],function(err, rows) {
    if (err) throw err;
    console.log('The solution is: ', rows[0].solution);
    });
  //res.send('iok');
  //connection.end();
});


app.listen(3000, function () {
  console.log('app listening on port 3000!');
});

这是我的代码:打字稿

import { Component, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
import { User } from './../User';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import {tap} from 'rxjs/operators';


@Component({
  selector: 'app-inscription',
  templateUrl: './inscription.component.html',
  styleUrls: ['./inscription.component.css']
})

export class InscriptionComponent implements OnInit {
  signupForm;
  results;
  //email = new FormControl('', [Validators.required, Validators.email]);

  constructor (private http: HttpClient, private formBuilder: FormBuilder)
  {}
  ngOnInit() {
    this.signupForm = this.formBuilder.group({
      email:"",
      name: "",
      lastname: "",
      password : "",
    });
  }
  public onFormSubmit() {

    const configUrl = 'http://localhost:3000/inscription';

    this.http.post(configUrl,this.signupForm.value)
    .pipe(
      tap(
        data => console.log(configUrl, data),
        error => console.log(configUrl, error)
      )
    )
    .subscribe(results => this.results = results);
 }
}

【问题讨论】:

  • app.get("/inscription"),应该是post not get

标签: node.js angular typescript express angular-material


【解决方案1】:

它有效,但我不明白为什么这次我不会恢复表单数据

<form [formGroup]="signupForm" class="example-form" (ngSubmit)="onFormSubmit(signupForm)"   novalidate>
  <mat-form-field class="example-full-width">
    <input type="text" ngModel id='email' name='email' matInput placeholder="Enter your email"/>
  </mat-form-field>

  <mat-form-field class="example-full-width">
          <input type="text" ngModel id='name' name='name' matInput placeholder="Nom">
  </mat-form-field>



  <mat-form-field class="example-full-width">
           <input  type="text" ngModel id='lastname' name='lastname' matInput placeholder="Prenom" >
  </mat-form-field>


  <mat-form-field class="example-full-width">
          <input type="text" ngModel id='password' name='password' type="password" matInput placeholder="Enter your password" [type]="hide ? 'password' : 'text'">
          <button mat-icon-button matSuffix (click)="hide = !hide" [attr.aria-label]="'Hide password'" [attr.aria-pressed]="hide">

          </button>
  </mat-form-field>


  <div class="example-button-row">
    <button type="submit" mat-raised-button>Inscription</button>
  </div>
</form>

【讨论】:

    【解决方案2】:

    你必须添加

    app.use(body.urlencoded({ extended: false }));
    app.use(body.json());
    

    就在app.use(cors());之后

    【讨论】:

      【解决方案3】:

      您需要使用正文解析器,以便您可以获取正文对象

      app.use(cors());
      app.use(express.json()); //Used to parse JSON bodies ? Express 4.16+
      

      你需要使用post方法

      app.post("/inscription",(req,res)=>{
       ...
      }
      

      阅读更多关于这个here ?

      【讨论】:

        【解决方案4】:

        你必须改变

        app.get("/inscription",(req,res)=>{
        

        app.post("/inscription",(req,res)=>{
        

        【讨论】:

        • 感谢您的回复,但错误仍然存​​在,我不明白
        猜你喜欢
        • 2021-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 2022-01-22
        • 2021-11-13
        • 2022-01-14
        相关资源
        最近更新 更多