Javascript 节点js类型错误:无法读取属性';车身';未定义的

Javascript 节点js类型错误:无法读取属性';车身';未定义的,javascript,node.js,express,Javascript,Node.js,Express,我正在为一门Udacity课程做一个示例项目,我被一些东西绊倒了。我试图从表单中捕获一些用户输入,并发出post请求以返回javascript对象,当我尝试使用node js运行服务器时,我得到错误: TypeError: Cannot read property 'body' of undefined 这是项目的服务器代码: server.js projectData = {}; /* Express to run server and routes */ const express =

我正在为一门Udacity课程做一个示例项目,我被一些东西绊倒了。我试图从表单中捕获一些用户输入,并发出post请求以返回javascript对象,当我尝试使用node js运行服务器时,我得到错误:

TypeError: Cannot read property 'body' of undefined
这是项目的服务器代码:

server.js

projectData = {};

/* Express to run server and routes */
const express = require('express');

/* Start up an instance of app */
const app = express();

/* Dependencies */
const bodyParser = require('body-parser')
/* Middleware*/
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const cors = require('cors');
app.use(cors());

/* Initialize the main project folder*/
app.use(express.static('project1'));

const port = 8000;
/* Spin up the server*/
const server = app.listen(port, listening);
 function listening(){
    // console.log(server);
    console.log(`running on localhost: ${port}`);
  };

// GET route

const animalData = [];

app.get('/all', getData);

function getData(req, res){
  res.send(AnimalData)
  console.log(AnimalData)
}

// function sendData (request, response) {
//  response.send(projectData);
// };

// POST route
app.post('/add', callBack);

function callBack(req,res){
  res.send('POST received');
}

// POST an animal
const data = [];


  // TODO-Call Function



app.route('/addAnimal')
  .get(function (req, res) {
    res.sendFile('index.html', {root: 'project1'})
  })
  .post(addAnimal())

function addAnimal(req, res){
  newEntry = {
    animal: req.body.animal,
    facts: req.body.fact,
    fav: req.body.fav
  }

  data.push(req.body);
  res.status(200).send(req.body);
  animalData.push(newEntry)
  res.send(animalData)
  console.log(animalData)
};
这是客户端的代码:

app.js

function performActuion(e){
const fav = document.getElementById('fav').value;



const getAnimal = async (url) =>{
  const res = await fetch(url);
  try {
    const data = await res.json();
    console.log(data)
    return data;
  } catch(error) {
    console.log()
  }
};

/* Function to POST data */
const postData = async ( url = '', data = {})=>{
    console.log(data);
      const response = await fetch(url, {
      method: 'POST', // *GET, POST, PUT, DELETE, etc.
      credentials: 'same-origin', // include, *same-origin, omit
      headers: {
          'Content-Type': 'application/json',
      },
      body: JSON.stringify(data), // body data type must match "Content-Type" header        
    });

      try {
        const newData = await response.json();
        console.log(newData);
        // console.log(newData);
        return newData.json()
        console.log(await response.json());
        return await response.json()
      }catch(error) {
      console.log("error", error);
      // appropriately handle the error
      };
  };

  // TODO-Call Function
  (async function(){
    let res = await postData('/addAnimal', (animal: data.animal, fact: data.fact, fav: fav));;
    console.log(res);
  })();
在本课的示例中,此代码似乎可以运行,但当我尝试在我的终端上运行它时,我甚至无法测试它,因为我得到了该类型错误。任何帮助都将不胜感激

谢谢,,
Mike

问题出在以下代码中:

app.route('/addAnimal')
  .get(function (req, res) {
    res.sendFile('index.html', {root: 'project1'})
  })
  .post(addAnimal())
app.route('/addAnimal')
  .get(function (req, res) {
    res.sendFile('index.html', {root: 'project1'})
  })
  .post(addAnimal)
您实际上是在调用函数,而不是将其用作回调,请使用以下代码: