【问题标题】:Why is Id only getting saved in database and no other data?为什么 Id 只保存在数据库中而没有其他数据?
【发布时间】:2019-12-17 12:14:47
【问题描述】:

我正在使用 Passport、sequelize 和 MySql 向我的 React 应用程序添加身份验证,但数据没有保存到 mySql 数据库中,只有 Id 和 null 字段。 我正在尝试实施本地策略。

在邮递员中尝试发布路线时,我收到 200 响应,其中包含电子邮件和哈希密码。但是,ID 字段显示为空。检查 mysql 数据库时,我可以看到添加了 ID 的新行,但用户名和密码为空。我在这里有什么遗漏吗?

护照策略文件

const Strategy = require('passport-local').Strategy;
const User = require('../models').User;
const bcrypt = require('bcryptjs');
const salt = bcrypt.genSaltSync(10);

const SignupStrategy = new Strategy({ passReqToCallback: true, 
usernameField: "email", passwordField: "password" }, function (req, email, 
password, done) {

User.findOne({
    where: {
        email: email
    }
})
    .then((user, error) => {
        if (error)
        return done(null, user);
    })
const encryptedPassword = bcrypt.hashSync(password, salt);
let newUser = new User({
    email,
    password: encryptedPassword
})
User.create(newUser)
    .then((user) => {
        newUser
        ;
        done(null, newUser)
    })
    .catch((error) => {
        done(error, null)
    })    
});

module.exports = SignupStrategy;

用户模型

 module.exports = function (sequelize, DataTypes) {
 var User = sequelize.define("User", {
 username: {
  type: DataTypes.STRING,
  // allowNull: false,
  unique: true,
  validate: {
    len: [1]
  }
 },
 email: {
    type: DataTypes.STRING,
  // allowNull: false,
    unique: true,
    validate: {
    isEmail: true
    }
  },
 password: {
  type: DataTypes.STRING,
  // allowNull: false
  }
  });

  return User;
 };

路线

  const express = require('express');
  const router = express.Router();
  const passport = require('../passport');

  router.post('/signup', (req, res, next) => {

      passport.authenticate('local-signup', function(error, user, info) {
          if (error) {
              res.status(500).json({
                  message: 'Ooops, something happened',
                  error: error.message || 'Internal server error'
              });
          }

          res.json({user})
      })(req, res, next)
  });

  router.post('/signin', function(req, res, next) {
      res.send('respond with a resource')
  });

  module.exports = router;

护照索引文件

const passport = require('passport');

const SignupStrategy = require('./SignupStrategy');
const SigninStrategy = require('./SigninStrategy');

passport.use('local-signin', SigninStrategy);
passport.use('local-signup', SignupStrategy);

module.exports = passport;

如果我在用户模型中将 allowNull 设置为 true,我会收到来自 sequelize 的验证错误(非空违规)。不知道现在该怎么办

【问题讨论】:

    标签: mysql reactjs sequelize.js passport.js


    【解决方案1】:

    而不是这个:

    let newUser = new User({
        email,
        password: encryptedPassword
    })
    

    使用这个:

    let newUser = {
        email,
        password: encryptedPassword
    };
    

    不需要创建任何特殊的对象来创建条目,它会 只需要简单的 json 对象。

    【讨论】:

    • @AlexYepes,如果它解决了您的问题,请接受答案并投票。
    • 对不起,由于我的初学者水平,我只能接受答案但还不能投票。我只能说:现在非常感谢:)
    • @AlexYepes,现在试试,你可以:)
    • @AlexYepes,快乐编码:D
    猜你喜欢
    • 2014-10-02
    • 2016-10-21
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多