So I'm currently taking Mosh Hamdani's Mastering React Course and the back-end development has given me a number of problems. The most recent one being error: “undefined No callback function was given”. I'm trying to register a new user using bcrypt.
To fix the problem I've tried using:
const salt = bcrypt.genSalt(10, function(_err, salt) {
bcrypt.hash(user.name, salt, function(_err, hash) {
// Store hash in your password DB.
});
});
from https://www.npmjs.com/package/bcrypt and even that didn't work.
If anyone could please help me it would be greatly appreciated.
Here's my user.js file:
const auth = require("../middleware/auth");
const bcrypt = require("bcrypt-nodejs");
const _ = require("lodash");
const { User, validate } = require("../models/user");
const express = require("express");
const router = express.Router();
router.get("/me", auth, async (req, res) => {
const user = await User.findById(req.user._id).select("-password");
res.send(user);
});
router.post("/", async (req, res) => {
const { error } = validate(req.body);
if (error) return res.status(400).send(error.details[0].message);
let user = await User.findOne({ email: req.body.email });
if (user) return res.status(400).send("User already registered.");
user = new User(_.pick(req.body, ["name", "email", "password"]));
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(user.password, salt);
await user.save();
const token = user.generateAuthToken();
res
.header("x-auth-token", token)
.send(_.pick(user, ["_id", "name", "email"]));
});
module.exports = router;
Here's a warning I was given in my terminal:
(node:7776) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. (node:7776) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. info: Listening on port 3900... (node:7776) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
Okay what I failed to realize earlier ago is that bcrypt-nodejs is no longer active https://www.npmjs.com/package/bcrypt-nodejs . Instead use bcryptjs https://www.npmjs.com/package/bcrypt .
Also I upgraded my code to:
bcrypt.genSalt(10, function (_err, salt) {
bcrypt.hash(user.name, salt, function (_err, hash) {
// Store hash in your password DB.
user.password = hash;
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.