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

297
Views
¿Qué es este error?: TypeError: No se puede leer la propiedad '0' de undefined

Al intentar crear la autenticación de usuario usando mongodb y express, encontré un error. El error básicamente ocurre cada vez que hago clic en enviar en el sitio web. Ese es el primer problema, el segundo es después de hacer clic en enviar, la página sigue cargando y nunca cambia como si estuviera atascada o atrapada en un bucle infinito. Intenté eliminar todo en la base de datos y comenzar de nuevo, pero eso no funcionó. Un pequeño consejo sería apreciado.

El error:

 app: authRouter { app: authRouter acknowledged: true, app: authRouter insertedId: new ObjectId("613b5afd2139aeac87bd9682") app: authRouter } +0ms app: authRouter TypeError: Cannot read property '0' of undefined app: authRouter at addUser (C:\Users\Yanki XXIV\Desktop\pluralsight\src\routers\authRouter.js:24:30) app: authRouter at processTicksAndRejections (internal/process/task_queues.js:95:5) +19ms

authRouter.js:

 const express = require('express'); const debug = require('debug')('app: authRouter'); const { MongoClient } = require('mongodb'); const authRouter = express.Router(); authRouter.route('/signUp').post((req, res) => { const {username, password} = req.body; const url = 'mongodb+srv://Yoshi:Yumcmaster1@cluster0.atic5.mongodb.net?retryWrites=true&w=majority' const dbName = 'testdb'; (async function addUser(){ let client try { let client = await MongoClient.connect(url); const db = client.db(dbName); const user = {username, password}; const results = await db.collection('users').insertOne(user); debug(results); req.login(results.ops[0], ()=> { res.redirect('/auth/profile'); }); } catch (error) { debug(error) } client.close(); }()); }); authRouter.route('/profile').get((req, res) => { res.json(req.user); }) module.exports = authRouter;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ciertamente no sé qué estás tratando de hacer porque no soy tan bueno en mongodb, pero encontré algo bastante interesante en los documentos del pasaporte js y stackoverflow.

Note: passport.authenticate() middleware invokes req.login() automatically. This function is primarily used when users sign up, during which req.login() can be invoked to automatically log in the newly registered user.

Check version of MongoDB Node.js Driver with command npm list mongodb. If it is version 4 then you can not access result.ops. In version 4 insertOne returns insertOneResult that have only 2 properties: acknowledged and insertedId.

NodeJS + MongoDB: insertOne() - obtenga el documento insertado de result.ops

http://www.passportjs.org/docs/login/

about 4 years ago · Juan Pablo Isaza 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!