Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
(node:19652) UnhandledPromiseRejectionWarning: ReferenceError: Cannot access 'password' before initialization

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 ??

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 :

  1. body-parser
  2. CORS

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'});
    }
})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!