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

299
Vistas
Unable to Fetch specific field from Mongodb using .find() Synatx ( Newbie)

I am newbee .Here is My Code. Bacically I am storing my jwt in Mongo DB and it is getting Properly stored. When I am trying to fetch that token to verify it. I am unable to fetch that token. And console.log(find_refreshtoken) is showing unexpected data instead of showing token. I want to fetch only tooken from this data.
Please help me. Thanks.

{
    _id: 621df700e560c645664a5b8a
    tooken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2NDYxMzA5NDQsImV4cCI6M..."
    user: 620cc33a4776c82d2f636399
    createdAt: 2022-03-01T10:35:44.270+00:00
    __v: 0
}

code Goes Here:

verifyRefreshToken: (refreshToken) =>{
        return new Promise((resolve,reject)=>{
            JWT.verify(refreshToken,process.env.Refresh_Token_Secret,(err,payload)=>{
                if(err)return reject(createError.Unauthorized())
                const userId=payload.aud
                const find_refreshtoken= RefreshTokenModel.find({"user":userId},{"tooken":1,"_id":0});
                console.log(find_refreshtoken);
                if(!find_refreshtoken){
                    reject(createError.InternalServerError())
                    return
                }
                if(find_refreshtoken === refreshToken){
                    console.log(find_refreshtoken);
                    return resolve(userId)
                }else{reject(createError.Unauthorized())} //It is executing this part
                
                //resolve(userId)
            })
        })
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

All you need is to read the documentation

The find method return a Query which means it is expected data not as you claim unxepected data.

So in order for your code to work just change:

const find_refreshtoken = RefreshTokenModel.find({"user":userId},{"tooken":1,"_id":0});

to:

const result = await RefreshTokenModel.find({"user":userId},{"tooken":1,"_id":0}).exec();

or even better use findOne, since you only want one i suppose:

const result = await RefreshTokenModel.findOne({"user":userId},{"tooken":1,"_id":0}).exec();
about 4 years ago · Juan Pablo Isaza 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