构建我的服务器时出现错误 [Route.get() 需要一个回调函数]

huangapple go评论158阅读模式
英文:

Error while building my server [Route.get() requires a callback function]

问题

我在构建服务器时遇到了错误,但我不知道如何修复。有没有办法修复这个错误?谢谢!我附上了服务器页面以及我的路由,这样你就可以看到我在做什么了。错误信息也在下面列出。非常感谢!

错误信息:

SyntaxError: 箭头函数参数列表格式错误

server.js:

const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();
const mongoose = require("mongoose");
const port = process.env.PORT || 5000;

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

const mongoURI = "";

mongoose
  .connect(mongoURI, { useNewUrlParser: true })
  .then(() => console.log('Mongodb connected'))
  .catch(err => console.log(err));

const Users = require('./routes/Users');
app.use('/users', Users);

app.listen(port, () => {
  console.log('Server is running on port.');
});

以下是routes/Users.js的更多代码:

const express = require("express");
const users = express.Router();
const cors = require("cors");
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");

const User = require("../models/User");
users.use(cors());

process.env.SECRET_KEY = 'secret';

users.post('/register', (req, res) => {
  const today = new Date();
  const userData = {
    first_name: req.body.first_name,
    last_name: req.body.last_name,
    email: req.body.email,
    password: req.body.password,
    created: today
  };
  User.findOne({
      email: req.body.email
    })
    .then(user => {
      if (!user) {
        bcrypt.hash(req.body.password, 10, (err, hash) => {
          userData.password = hash;
          User.create(userData)
            .then(user => {
              res.json({ status: user.email + 'registered' });
            })
            .catch(err => {
              res.send('error: ' + err);
            });
        });
      } else {
        res.json({ error: 'User already exists' });
      }
    })
    .catch(err => {
      res.send('error:' + err);
    });
});

module.exports = users;

希望这有助于解决你的问题!如果还有其他问题,请随时提问。

英文:

I'm getting an error while building my server, which I have no idea how to fix. Any idea on how to fix this? Thank you! I'm attaching the server page but also my routes so you can see what I'm doing. The error is also listed below. Thank yo uso much!

Error:
users.post('/register' (req, res) => {
^^^^^^^^^

SyntaxError: Malformed arrow function parameter list

server.js

const express = require("express");
const cors= require("cors");
const bodyParser = require("body-parser")
const app = express();
const mongoose = require("mongoose");
const port = process.env.PORT || 5000


app.use(bodyParser.json())
app.use(cors())
app.use(
  bodyParser.urlencoded({
    extended: false
  })
)

const mongoURI = ""

mongoose
  .connect(mongoURI, {useNewUrlParser: true})
  .then(() => console.log('Mongodb connected'))
  .catch(err => console.log(err)) 

  const Users = require('./routes/Users')
  app.use('/users', Users)

  app.listen(port, () => {
    console.log('Server is running on pol.')
  }) 

Here is more code from routes.
routes/Users.js

const express = require("express")
const users = express.Router()
const cors = require("cors")
const jwt = require("jsonwebtoken")
const bcrypt = require("bcrypt")

const User = require("../models/User")
users.use(cors())

process.env.SECRET_KEY = 'secret'

users.post('/register' (req, res) => {
    const today = new Date();
    const userData = {
        first_name: req.body.first_name,
        last_name: req.body.last_name,
        email: req.body.email,
        password: req.body.password,
        created: today
    }
    User.findOne({
        email: req.body.email
    })
    .then(user => {
        if(!user) {
            bcrypt.hash(req.body.password, 10, (err, hash) => {
                userData.password = hash
                User.create(userData)
                .then(user => {
                    res.json({status: user.email + 'registered'})
                })
                .catch(err => {
                    res.send('error: ' + err)
                })
            })
        } else {
            res.json({error: 'User already exists'})
        }
    })
    .catch(err => {
        res.send('error:' err)
    })
})

module.export = users

答案1

得分: 1

更改:

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

为:

users.post('/register', (req, res) => {
英文:

Its a simple typo mistake

change

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

TO

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

huangapple
  • 本文由 发表于 2020年1月6日 18:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定