I don't know how should i resolve this error..
also , i got a warning which says: Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use node --trace-warnings ... to show where the warning was created)
const express =require('express');
const mongoose = require('mongoose');
const path = require('path');
const User = require('./model/user.js')
const bcrypt = require('bcryptjs');
mongoose.connect('mongodb://localhost:27017/login-app-db',{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
const app = express();
app.use('/', express.static(path.join(__dirname, 'static')));
app.use(express.json());
app.post('/api/register', async(req, res)=>{
console.log(req.body);
const {username, password: plainTextPassword}= req.body;
const password = await bcrypt.hash(password, 10);
try {
const response = await User.create({
username,
password
})
console.log('User created successfully:', response)
} catch (error) {
console.log(error);
return res.json({ status: 'Error'});
}
console.log(await bcrypt.hash(password, 10));
res.json({status: 'ok'})
})
app.listen(9999, () => {
console.log('Server Running at \'localhost:9999\'');
})
my package dependencies:
"bcryptjs": "^2.4.3",
"express": "^4.17.1",
"mongodb": "^3.6.3",
"mongoose": "^5.11.15",
"nodemon": "^2.0.7",
"path": "^0.12.7"
}
any hints ??
Make sure you parse data from your front. And edit your code and make it simple and less verbose.
Tips : At last you use these :
This should work.
app.post('/api/register', async(req, res)=>{
req.body.password = bcrypt.hashSync(req.body.password, 10)
const response = new User(req.body)
try {
await response.save()
return res.status(200).json({status : 'ok'})
} catch (error) {
console.log(error);
return res.json({ status: 'Error'});
}
})