Node.js Express.js MongoDB:route.post()需要回调函数,但有一个[object Undefined]

时间:2018-07-03 11:31:14

标签: javascript node.js mongodb express

我正在尝试设置MEAN(mongodb,express,nodejs,angular6(CLI))应用程序。尝试将用户注册表单数据发布到mongo数据库,但引发错误。这是我第一个尝试学习的MEAN应用。

我在这里想念什么吗?

  

错误:Route.post()需要回调函数,但有一个[object Undefined]

server.js

这里是server.js个文件。

var express = require('express');
var bodyParser =  require('body-parser');
var path = require('path');
var http = require('http');

var app = express();

var api = require('./server/routes/api');

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

app.use(express.static(path.join(__dirname + '/dist/meanshopcart')));

app.use('/api', api);

app.post('/sign-up', api.signup);

app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname, '/dist/meanshopcart/index.html'));
});

var port = process.env.PORT || '3000';
app.set('port', port);

var server = http.createServer(app);
//app.listen(port, function(){
//    console.log("Server is running..!!");
//});

server.listen(port, function(err){
    if(err){
       return console.log('something bad happened', err);
    }
    console.log("Server is running..!!");
});

路由文件夹(routes / api.js)

我的路线文件夹./server/routes/api.js

var express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
const User = require('../models/users');

const db = 'mongodb://localhost:27017/meanshopapp';
mongoose.connect(db, function(err){
    console.log("mongo connection done");
    if(err){
        console.log("Error.."+err);
    }
});

router.get('/', (req, res)=>{
    console.log("get api ");
    User.find({}, function(err, users) {
    if (err) throw err;

  // object of all the users
   console.log(users);
  });
    res.send(users);
});

exports.signup = function(req, res, next){
    console.log("new user entered");

    var newUser = new User();
    newUser.name = req.body.name;
     newUser.email = req.body.email;
     newUser.password = req.body.password;
     newUser.phoneNo = req.body.phone;
     newUser.address = req.body.address;
    
    newUser.save(function(err){
        if(err){
            console.log("error saving user");
        }
        else{
            console.log("user inserted");
        }
    })
};

module.exports = router;

模型

这是我的用户架构user.js

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const userSchema = new Schema({
  name : { type: String },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  phoneNo: { type: Number },
  address: { type: String }
});

module.exports = mongoose.model('user', userSchema, 'users');

2 个答案:

答案 0 :(得分:0)

尝试更改./server/routes/api.js中的关注

function signup(req, res, next){
console.log("new user entered");

var newUser = new User();
newUser.name = req.body.name;
 newUser.email = req.body.email;
 newUser.password = req.body.password;
 newUser.phoneNo = req.body.phone;
 newUser.address = req.body.address;

newUser.save(function(err){
    if(err){
        console.log("error saving user");
    }
    else{
        console.log("user inserted");
    }
  })
};

module.exports = {router, signup};

它应该完成工作

答案 1 :(得分:0)

您正在使用导出和module.exports。 您要么应该遵循@dimagolovin的回答 要么 module.exports = {路由器};

并使用

`app.use('/ api',api.router);

app.post('/ sign-up',api.signup);`