Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
(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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda