【问题标题】:How can I solve this error TypeError: Users is not a function我该如何解决这个错误 TypeError: Users is not a function
【发布时间】:2016-05-17 21:24:04
【问题描述】:

我该如何解决这个错误 TypeError: Users is not a function

var express = require('express');
var router = express.Router();

var Users = require('../models/user');

router.post('/signup', function(req, res){
    // res.send('Ok');
    var name = req.body.name;
    var surname = req.body.surname;
    var email = req.body.email;
    var username = req.body.username;
    var password = req.body.password;
    var confirm_password = req.body.confirm_password;

    req.checkBody('name', 'Name is required').notEmpty();
    req.checkBody('surname', 'Surname is required').notEmpty();
    req.checkBody('email', 'Email address is required').notEmpty();
    req.checkBody('email', 'Invalid email address').isEmail();
    req.checkBody('username', 'Username is required').notEmpty();
    req.checkBody('password', 'Password is required').notEmpty();
    req.checkBody('confirm_password', 'Passwords do not match').equals(req.body.password);

    var errors = req.validationErrors();
    if(errors){
        res.json({status: errors})
    }else{
        var newUser = new Users({
            name:name,
            surname:surname,
            email:email,
            username:username,
            password:password
        })
        Users.createUser(newUser, function(err, user){
            if(err){ throw err};
            console.log(user);
        });
        res.json({status:"success"})
    }
})
router.get('/signin', function(req, res){
    res.send('Sure');
})

module.exports = router;

我试图在我的 mongodb 中插入一个新用户,但我不断收到用户不是函数的错误,我的上面的代码在我的路由文件夹中,下面的代码是我的架构

        var mongoose = require('mongoose')
    var Schema = mongoose.Schema;
    var bcrypt = require('bcryptjs');

    var usersSchema = new Schema({
        name:{
            type:String,
            required:true
        },
        surname:{
            type:String,
            required:true
        },
        email:{
            type:Number
        },
        username:{
            type:Number
        },
        password:{
            type:Number
        },
        create_date:{
            type:Date,
            default:Date.now
        }
    })

    var Users =  mongoose.model('Users', usersSchema);

    module.exports.createUser = function(newUser, callback){
        // hashing the passwords
        bcrypt.genSalt(10, function(err, salt){
            bcrypt.hash(newUser.password, salt, function(err, hash){
                newUser.password = hash;
                 newUser.save(callback);
            })
        })

    }

【问题讨论】:

  • 嗨,姆林多斯。你从哪里得到错误?你在很多地方都有引用用户

标签: node.js mongodb


【解决方案1】:

您必须导出用户模型。 阅读mongoose guide

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcryptjs');

var usersSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    ...
})


usersSchema.statics.createUser = function (newUser, callback) {
    ...
}


module.exports =  mongoose.model('Users', usersSchema)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2018-09-01
    • 2018-11-14
    • 2021-07-02
    相关资源
    最近更新 更多