Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8676655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:15:06+00:00 2026-06-12T20:15:06+00:00

I have a findOne query, and when ever i verify if it returned a

  • 0

I have a findOne query, and when ever i verify if it returned a empty document i get thrown a error saying ‘cannot read property ‘username’ of null’. That happend when i try to acess doc.username in if(!doc.username) {

My code:

function checkAccDb(username, password) { console.log(2);
    /* Check if accounts exists in db */
    db.findOne({username: username}, function(err, doc){ console.log(3);
        if(err) throw err;

        if(!doc.username) {
            add2stack(username, password);
        }
        else if(doc.status == 200) {
            end(username, password, 1000);
        }
        else if(doc.status == 401) {
            if(doc.password == password)
                end(username, password, 401);
            else
                add2stack(username, password);
        }
        else {
            add2stack(username, password);
        }
    });
}

Could anyone please explain me what’s happening here?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:15:07+00:00Added an answer on June 12, 2026 at 8:15 pm

    The query succeeds but doesn’t find any matches, so both err and doc are null. You need to check if doc is null and handle that case appropriately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using the npm mongodb driver with node. I have collection.findOne({query}, function(err, result) {
I have Document in MongoDB like this: {ONE: {TWO: {THREE:5}}} I want to query
I have this document stored in mongodb document: { _id:ObjectId(4eb7642ba899edcc31000001) hash:abcd123 value:some_text_here } I
I'm working with the MongoDB official driver (10Gen). And I cannot query a MonogoDBRef
i use propel for database interaction. Now i have to create a query like
I have a mongodb function call that return a user object User.findOne(condition, function(err, user)
i'm have a collection with records like this > db.company.findOne({companyId:1}) { _id : ObjectId(4e22ff08eefdd839f60ab95f),
Basic problem I have a bunch of records and I need to get latest
I have a query in Delphi using DBExpress TSQLQuery that looks like so ActiveSQL.sql.add('SELECT
I have set up my database so that the ID property of my class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.