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
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 answers
Answer question

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