【问题标题】:need assistance on mongoose model在猫鼬模型上需要帮助
【发布时间】:2019-07-17 05:22:32
【问题描述】:

我是 mongoDB 和 mongoose 的新手。但是我想做的是能够通过他们的电子邮件访问我存储在数据库中的用户,如果成功检索到用户,我会将他们写的密码与存储在数据库中的哈希密码进行比较

这就是我的架构所拥有的

用户架构

var UserSchema = new Schema({
  firstName: { type: String, required: true }, //require makes it so that the fields can't be left blank
  lastName: {type: String, required: true},
  emailAddress: {type: String, required: true},
  password: {type: String, required: true}      
});
var User = mongoose.model("User", UserSchema);

我在我的 routes.js 文件中使用 basic-auth 包以便在邮递员中测试这个 api,这是我卡住的部分,const user = User.find({' emailAddress': credentials.name, user.emailAddress} ); 我无法组合查询以访问数据库中的用户电子邮件

    //This middle-where function will authenticate users
const authenticateUser = (req, res, next) => {
  let message = null;

  // Parse the user's credentials from the Authorization header.
  const credentials = auth(req);

  // If the user's credentials are available...
  if (credentials) {
    // Attempt to retrieve the user from the data store
    // by their email (i.e. the user's "key"
    // from the Authorization header).
    const user = User.find({'emailAddress': credentials.name, user.emailAddress} );

    // If a user was successfully retrieved from the data store...
    if (user) {
      // Use the bcryptjs npm package to compare the user's password
      // (from the Authorization header) to the user's password
      // that was retrieved from the data store.
      const authenticated = bcryptjs
        .compareSync(credentials.pass, user.password);

在这个快速路由器中,只有经过身份验证后,我才会返回用户

//GET /api/users 200, THIS WORKS IN POSTMAN
//This Route returns the currently authenticated user,     
router.get('/users', authenticateUser, (req, res) => {
  //within the route handler, the current authenticated user's information is retrieved from the Request object's currentUser property:
  const user = req.currentUser;
//we use the Response object's json() method to return the current user's information formatted as JSON:
  res.json({
    firstName: user.firstName,
    lastName: user.lastName,
  });
});

有人可以帮忙吗?作为参考,这是我的回购https://github.com/SpaceXar20/rest_api-mongo-p9

【问题讨论】:

    标签: database mongodb express mongoose


    【解决方案1】:

    这里你 find() 的方式是错误的 它应该是回调或带有异步等待的 exec() .. 这种情况下只需使用回调 所以代替这段代码,

    const user = User.find({'emailAddress': credentials.name, user.emailAddress} );
    

    使用此代码

        User.find({emailAddress:user.emailAddress},(err,user)={
          if(err) throw err;
          // do what you please
        if (user) {
    
          bcrypt.compare(password,hash,function(err,isMatch){
        if(err) callback(err,null);
        callback(null,isMatch);
    
    });
        } );
    

    【讨论】:

    • 我尝试了你的建议,但我最终得到一个错误,用户未在邮递员上定义
    • 你能展示一下你是如何使用 bcrypt 作为回调的吗?
    • ok ..我以为你忘了使用回调..但我看到你已经在其中使用了 Sync() 方法..但无论如何尝试这里我给出的回调......再次检查代码..我已经编辑了回调
    • 啊,是的,我在 github 上对我的代码进行了一些更新
    • 是的.. 再试一次 bcrypt 回调伙伴
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 2015-09-16
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多