错误:需要数据和salt参数(异步)

时间:2018-01-14 15:04:40

标签: node.js mongoose bcrypt

我在使用Schema.pre时遇到了问题('保存'),在我的模型'用户'中,无法获得'这个'用bcrypt对我的密码进行哈希扫描。

我的app.js,使用mongoose在mongodb中进行简单连接

#include <boost/polygon/point_traits.hpp>
#include <boost/polygon/polygon.hpp>
#include <boost/polygon/polygon_set_data.hpp>
#include <boost/polygon/polygon_set_traits.hpp>
#include <boost/polygon/polygon_traits.hpp>

namespace gtl = boost::polygon;
using namespace boost::polygon::operators;

typedef gtl::polygon_90_data<int> Polygon;
typedef gtl::polygon_traits<Polygon>::point_type Point;
typedef gtl::polygon_90_set_data<int> PolygonSet;

Polygon make_polygon(std::initializer_list<Point> init) {
    Polygon p;
    p.set(init.begin(), init.end());
    return p;
}

template <typename P>
P get_union(P const& a, P const& b) {
    std::vector<P> v;
    PolygonSet(a+b).get(v);
    assert(v.size() == 1);
    return std::move(v.front());
}

int main() {
    Polygon u = get_union(
       make_polygon({ {100, 200}, {120, 200}, {120, 140}, {100, 140} }),
       make_polygon({ {115, 180}, {115, 250}, {140, 250}, {140, 180} }));

    for (auto& pt : u)
        std::cout << "{" << pt.x() << ", " << pt.y() << "} ";
}

我的路线,我在我的应用程序中创建了创建用户的路线,但在预先保存&#39;无法得到这个&#39;哈希密码。

mongoose.connect('mongodb://localhost/gederson', {
  useMongoClient: true,

});

const db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
  console.log('Connected on mongo');
});

app.listen(process.env.PORT || 3000, () => {
  console.log('listening');
});

index(app, db);
post(app, db);
admin(app, db);


module.exports = app;

我的模型用户,代码预先&#39;保存&#39;

const Users = db.model('Users');

const newUser = {
          username: req.body.username,
          email: req.body.email,
          password: req.body.password,
        };
        Users.create(newUser, (err) => {
          if (err) throw err;
          res.status = 201;
          return res.send('User created');
        });

堆叠错误:

const bcrypt = require('bcrypt');

UserSchema.pre('save', (next) => {
  const user = this;
  bcrypt.hash(user.password, 10, (err, hash) => {
    if (err) {
      return next(err);
    }
    user.password = hash;
    return next();
  });
});

const Users = mongoose.model('Users', UserSchema);

module.exports = Users;

2 个答案:

答案 0 :(得分:0)

我今天遇到了类似的问题。

我通过删除ES6语法(箭头功能)解决了这个问题。

UserSchema.pre('save', function(next) {
  const user = this;
  bcrypt.hash(user.password, 10, function(err, hash) {
     if (err) {
     return next(err);
     }
     user.password = hash;
     next();
  })
});

答案 1 :(得分:0)

因为要在该函数上传递输入的空白​​值

如果对邮递员进行测试,或者需要输入数据 enter image description here

{
    "name":"test",
    "email":"test@test.com",
    "password":"123456"
}

添加请参阅我的数据存储区代码示例


router.post("/register-user", (req, res, next) => {
    bcrypt.hash(req.body.password, 10).then((hash) => {
        const user = new userSchema({
            name: req.body.name,
            email: req.body.email,
            password: hash
        });
        user.save().then((response) => {
            res.status(201).json({
                message: "User successfully created!",
                result: response
            });
        }).catch(error => {
            res.status(500).json({
                error: error
            });
        });
    });
});