# Problem with Database querying in NodeJS

**URL:** <https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399>\
**Category:** General\
**Created:** [September 2, 2017, 4:52pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399 "2017-09-02T16:52:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamza-hajji](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hamza-hajji/32/70730_2.png) [@hamza-hajji](https://forum.freecodecamp.org/u/hamza-hajji)\
**Post date:** [September 2, 2017, 4:52pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/1 "2017-09-02T16:52:41Z")

</div>

I’m working on the URL Shortener API, I store the data in the database and when i try to retrieve it with a get route, this happens:  
this is the code

```javascript
app.route('/:short_url_index')
  .get(function (req, res) {

    MongoClient.connect(Mongo_URI, function (err, db) {
      if (err) return console.log('Error connecting to the Database', err);

      var short_url_index = parseInt(req.params.short_url_index);

      db.collection('urls').findOne({
        short_url_index: short_url_index
      }, function (err, doc) {
        res.redirect(doc.original_url);
      });
      db.close();
    });

  });

```

i’m basically trying to query the database by the short url and get the original url, but i get this error in the cmd:

```auto
Node.js listening ...
*
*
C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb\lib\utils.js:123
    process.nextTick(function() { throw err; });
                                  ^

TypeError: Cannot read property 'original_url' of null
    at C:\Users\hamza\Desktop\url_shortener\server.js:100:25
    at handleCallback (C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb\lib\utils.js:120:56)
    at C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb\lib\collection.js:1417:5
    at handleCallback (C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb\lib\utils.js:120:56)
    at C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb\lib\cursor.js:682:5
    at handleCallback (C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb-core\lib\cursor.js:171:5)
    at setCursorNotified (C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb-core\lib\cursor.js:515:3)
    at C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb-core\lib\cursor.js:590:16
    at queryCallback (C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb-core\lib\cursor.js:232:18)
    at C:\Users\hamza\Desktop\url_shortener\node_modules\mongodb-core\lib\connection\pool.js:469:18
[nodemon] app crashed - waiting for file changes before starting...

```

but when i try to log the document retrieved from the database, i get it correctly and it’s not undefined

---

<div class="post-metadata">

**Author:** ![ppc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ppc/32/63349_2.png) [@ppc](https://forum.freecodecamp.org/u/ppc)\
**Post date:** [September 2, 2017, 5:33pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/2 "2017-09-02T17:33:38Z")

</div>

findOne callback needs to check the error parameter - it will show the problem

---

<div class="post-metadata">

**Author:** ![hamza-hajji](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hamza-hajji/32/70730_2.png) [@hamza-hajji](https://forum.freecodecamp.org/u/hamza-hajji)\
**Post date:** [September 2, 2017, 5:43pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/3 "2017-09-02T17:43:03Z")

</div>

Added the error check, and i’m still having the same problem

```javascript
if (err) res.status(400).send({error: err});

```

---

<div class="post-metadata">

**Author:** ![ppc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ppc/32/63349_2.png) [@ppc](https://forum.freecodecamp.org/u/ppc)\
**Post date:** [September 2, 2017, 5:47pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/4 "2017-09-02T17:47:38Z")

</div>

what does the error say? since there is an error you cannot use the `doc` parameter

---

<div class="post-metadata">

**Author:** ![hamza-hajji](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hamza-hajji/32/70730_2.png) [@hamza-hajji](https://forum.freecodecamp.org/u/hamza-hajji)\
**Post date:** [September 2, 2017, 5:49pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/5 "2017-09-02T17:49:26Z")

</div>

It just throws the same error: and i don’t see any response in postman

---

<div class="post-metadata">

**Author:** ![ppc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ppc/32/63349_2.png) [@ppc](https://forum.freecodecamp.org/u/ppc)\
**Post date:** [September 2, 2017, 5:55pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/7 "2017-09-02T17:55:42Z")

</div>

the problem is db.close() is called right after findOne which is async

---

<div class="post-metadata">

**Author:** ![hamza-hajji](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hamza-hajji/32/70730_2.png) [@hamza-hajji](https://forum.freecodecamp.org/u/hamza-hajji)\
**Post date:** [September 2, 2017, 5:56pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/8 "2017-09-02T17:56:57Z")

</div>

what’s the solution to this? should i use promises?

---

<div class="post-metadata">

**Author:** ![hamza-hajji](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hamza-hajji/32/70730_2.png) [@hamza-hajji](https://forum.freecodecamp.org/u/hamza-hajji)\
**Post date:** [September 2, 2017, 6:02pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/9 "2017-09-02T18:02:29Z")

</div>

Here’s what i did:

```auto
db.collection('urls').findOne({
        short_url_index: short_url_index
      }).then(function (doc) {
        res.redirect(doc.original_url);
        console.log(doc);
      }).catch(function (err) {
        res.send({error: err});
      });
      db.close();

```

and this is the response

```auto
{ _id: 59aadc0b918df030bcc216d7,
  original_url: 'www.google.com',
  short_url_index: 3432,
  short_url: 'http://localhost:3000/3432' }

```

this makes it way more confusing, if i can get the object back, why can’t i get its property?

---

<div class="post-metadata">

**Author:** ![ppc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ppc/32/63349_2.png) [@ppc](https://forum.freecodecamp.org/u/ppc)\
**Post date:** [September 2, 2017, 6:03pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/10 "2017-09-02T18:03:41Z")

</div>

you cannot close the db until the work is done - the work is done in the `findOne` callback - so close must be called in the callback

promises make async code look cleaner - arguably they make async solutions easier but you must understand promises first - and the best use of promises now is with async/await supported in node

---

<div class="post-metadata">

**Author:** ![ppc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ppc/32/63349_2.png) [@ppc](https://forum.freecodecamp.org/u/ppc)\
**Post date:** [September 2, 2017, 6:16pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/11 "2017-09-02T18:16:01Z")

</div>

your promises version has the same async logic problem aka race condition - a little easier to fix with promises - put `db.close()` in a final then

in the future there may be a promises `finally`

---

<div class="post-metadata">

**Author:** ![hamza-hajji](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hamza-hajji/32/70730_2.png) [@hamza-hajji](https://forum.freecodecamp.org/u/hamza-hajji)\
**Post date:** [September 2, 2017, 6:21pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/12 "2017-09-02T18:21:07Z")

</div>

Even if i put the db.close() call into the callback, it does the same thing.  
I don’t think how i handle the async is important because event if i comment out the close call it does the same thing.  
and this is the response i get from the catch() call

```auto
{
    "error": {}
}

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:59pm UTC](https://forum.freecodecamp.org/t/problem-with-database-querying-in-nodejs/145399/13 "2021-01-16T17:59:37Z")

</div>

Closed on January 16, 2021, 5:59pm UTC


