node.js - Express 无法从后面读取未定义的属性 'name'

标签 node.js angular typescript express angular-material

我尝试使用 Node js 和 Angular(Material ui)开发一个全栈应用程序。

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

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

我收到以下错误:

TypeError: Cannot read property 'name' of undefined

这是我的代码:

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!');
});

这是我的代码: typescript

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.use(cors());
app.use(express.json()); //Used to parse JSON bodies 👉 Express 4.16+

并且需要使用post方法

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

了解更多相关信息here 🚀

关于node.js - Express 无法从后面读取未定义的属性 'name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59485625/

相关文章:

node.js + express 3 + socket.io = 发送后无法设置 header

angular - 我从 GET 方法收到的数据没有在 html 页面上更新

typescript - 使用带有 typescript 的身份对象代理和开 Jest 时返回未定义

javascript - Textarea 的类型 'value' 上不存在属性 'HTMLElement' - Angular

javascript - 如何使用 NodeJs 流过滤 JSON 文件

node.js - 显示没有 layout.jade 文件的部分

node.js - 使用 NodeJS 和 Mongoose 查询嵌入式文档

angular - 以 Angular 形式添加动态控件失败

angular - typescript 中的索引签名如何工作?

javascript - 如何在异步生成器函数中引发错误