【问题标题】:Unexpected token . on module.exports.function意外的标记 。在 module.exports.function 上
【发布时间】:2017-12-13 10:22:26
【问题描述】:

我正在尝试创建一个 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);
        });
    });
}

【问题讨论】:

    标签: javascript express mongoose


    【解决方案1】:

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

    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);
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 2018-08-02
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      相关资源
      最近更新 更多