意外的标记 。在module.exports.function上

时间:2017-12-13 10:22:26

标签: javascript express mongoose

我正在尝试创建一个API注册路由,但是当我尝试使用User.function - 我在模型文件中创建的AddUser时,它表示存在意外的令牌.。这是代码:

router.post('/register', (req, res, next) => {
    let newUser = new User({
        username: req.body.username,
        name: req.body.name,
        email: req.body.email,
        password: req.body.password,
        photoUrl: req.body.photoUrl
    })

    User.addUser(newUser, (err, user) => {
        if (err) {
            res.send('Failed');
        } else {
            res.send('Registered');
        }
    });
});

以下是模型文件代码:

const mongoose = require('mongoose');
const schema = mongoose.Schema;
const bcryptjs = require('bcryptjs');

const userSchema = new schema({
    username: { type: String, required: true },
    name: { type: String },
    email: { type: String, required: true },
    password: { type: String, required: true },
    photoUrl: { type: String }
});

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

module.exports.addUser(newUser, callback) => {
    bcrypt.genSalt(10, (err, salt) => {
        bcrypt.hash(newUser.password, salt, (err, hash) => {
            if (err) throw err;
            newUser.password = hash;
            newUser.save(callback);
        });
    });
}

1 个答案:

答案 0 :(得分:0)

在模型文件代码中,添加=,如下所示

module.exports.addUser = (newUser, callback) => {
    bcrypt.genSalt(10, (err, salt) => {
        bcrypt.hash(newUser.password, salt, (err, hash) => {
            if (err) throw err;
            newUser.password = hash;
            newUser.save(callback);
        });
    });
}