简体   繁体   English

nodejs仅能在数据库中创建一个用户

[英]nodejs only able to create one user in the database

I'm using an api route in nodejs to add a user to the database with mongoose. 我在nodejs中使用api路由,使用猫鼬将用户添加到数据库中。 Standard stuff. 标准的东西。 However, it works perfectly with one user. 但是,它只适合一个用户使用。 Then when I try to create another user the new user is logged in but not saved to the database. 然后,当我尝试创建另一个用户时,新用户已登录但未保存到数据库。

Any help is appreciated. 任何帮助表示赞赏。 Here is the route code: 这是路线代码:

    app.get('/register' , function(req , res) {
    res.sendFile(path.join(__dirname , '../public' , 'register.html'));
});
app.post('/api/register' , function(req, res) {
    User.findOne({'local.email': req.body.email}, function(err, user) {
        if(err) {
            return res.json({
                'message': err
            });
        }
        if(user) {
            return res.json({
                'message': 'User already exists'
            });
        } else {
            let newUser = new User();

            newUser.local.email = req.body.email;
            newUser.local.name = req.body.name;
            newUser.local.password = newUser.setPassword(req.body.password);

            newUser.save(function(err) {
                var token;
                token = newUser.generateJwt();
                res.status(200);
                res.json({
                    'token': token
                });
            });
        }
    });
});

Schema: 架构:

const bcrypt = require('bcrypt-nodejs');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
let userSchema = new mongoose.Schema({
    local: {
        email: String,
        name: String,
        password: String
    }
});
userSchema.methods.setPassword = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(10), null);
}
userSchema.methods.validatePassword = function(password) {
    return bcrypt.compareSync(password, this.local.password);
}
userSchema.methods.generateJwt = function() {
  var expire = new Date();
  expire.setDate(expire.getDate() + 7);

  return jwt.sign({
    _id: this._id,
    email: this.local.email,
    name: this.local.name,
    exp: parseInt(expire.getTime() / 1000),
  }, process.env.CONFIG_SR);
};
module.exports = mongoose.model('User' , userSchema);

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. let允许您声明范围仅限于使用它的块,语句或表达式的变量。 This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope. 这与var关键字不同,该关键字在全局范围内或在整个函数本地定义变量,而不管块范围如何。

let newUser = new User(); 

change this to 更改为

var newUser = new User():

Turns out this was an issue with mLab. 原来这是mLab的问题。 To fix it, I deleted the users collection and registered a new user. 为了解决这个问题,我删除了用户集合并注册了一个新用户。 After the users collection was generated again, I was able to add new users to the database. 再次生成用户集合后,我便可以将新用户添加到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM