使用 Javascript 语法并使用 Postman 测试 API

while using Javscript syntax and Testing API using Postman

各位专家大家好,我正在尝试使用以下 javaScript 代码连接到 postamn,

然而,当我 运行 nodeman 时,正常连接似乎没问题,当我向邮递员发送 GET 请求时也没有错误,确实工作正常,我不知道我做错了什么在 POST API 请求下,我尝试使用 ES6 早期语法广告,就像我现在使用 ES6 一样,但似乎不起作用。

请在下面找到我的 app.js 文件中的代码

    const express = require ('express');
const app = express();
const bodyParser = require('body-parser')

const mongoose = require('./database/mongoose');
const user = require('./database/models/user');


/*
install CORS: Cross Origin Request Security
This is basically used to run two ports on same server, 
i.e Front-End: running on localhost:4200 which is using angular framework
BackEnd: localhost: running on 3000- backend api using nodejs, express
*/
app.use(bodyParser.json());

app.use ((req, res, next) =>{
  res.header("Access-Control-Allow-Orgin", "*");
  res.header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PATCH< DELETE" );
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
  
});

// app.use(express.json());
// app.get('/user', (req, res) => {
  //     person.find({})
  //     .then(user => res.send(user))
  //     .catch ((error) => console.log(error));
  // })
  app.get('/', (req, res)=> {
      res.send("hello world");
  });


  app.post('/register', (req, res) => {

    // (new User ({
    //   'firstname': req.body.firstname,
    //   'lastname': req.body.lastname,
    //   'email': req.body.email,
    //   'password': req.body.password
    // }))

    // .save()
    // .then((user) => res.send(user))
    //  .catch((error) => console.log(error));
    const firstname = req.body.firstname;
    const lastname = req.body.lastname;
    const email = req.body.email;
    const password = req.body.password;
    
    
    const user = new User();
    user.firstname = firstname;
    user.lastname = lastname;
    user.email = email;
    user.password = password;
    
    user.save((err, result ) => {
      if(err){
        console.log("There is error adding user to database");
        res.sendStaus(500);
      }
      res.sendStatus(200);
    })
    
  });
  
  
  app.listen(3000, () => console.log("Server is Connected to port 3000"));

另外请问我遇到的错误,我也是这个开发者的新手,有人可以纠正我吗?

ReferenceError: User is not defined
at C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\app.js:54:18
at Layer.handle [as handle_request] (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\index.js:275:10)
at C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\app.js:21:3
at Layer.handle [as handle_request] (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\index.js:317:13)
at C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\express\lib\router\index.js:275:10)
at C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\body-parser\lib\read.js:130:5
at invokeCallback (C:\Users\tapesh.patel\Desktop\Website\Kare-app\BackEnd\node_modules\raw-body\index.js:224:16)

我认为 Jason 可能存在解析问题,所以我在使用 express.json 将代码解析为 json 时使用了 Bodyparser。我猜这两个在做同样的工作。

仍然不知道 JavaScript 缺少什么。如果有高手指正,帮助学习,将不胜感激

谢谢 塔佩什

您已经导入了名称为 user 的架构,而您正试图使用​​名称为 User

的架构