Amr Elmohamady
Amr Elmohamady

Reputation: 306

Node GET request is hanging unable to finish the request and the page keep loading

SOLVED

I am currently building an app with Node - Express - MongoDB - ejs.

I have a books router that should render the books ejs file and books list from MongoDB. Here's the books.js code:

const express = require("express");
const router = express.Router();
const { MongoClient } = require("mongodb");

router.route("/").get(async (req, res, next) => {
  const url = "mongodb://localhost:27017";
  const dbName = "Library";
  let client;
  try {
    client = await MongoClient.connect(url, { useUnifiedTopology: true });
    const db = client.db(dbName);
    const books = await db.collection("books").find().toArray();

    res.render({
      title: "Books",
      books,
    });
  } catch (err) {
    res.send(err);
  }
});

module.exports = router;

It is supposed to get the data from the MongoDB and send it back to the books page instead I get this error { "code": "ERR_INVALID_ARG_TYPE" }

Upvotes: 0

Views: 251

Answers (2)

Quentin
Quentin

Reputation: 943142

You define a function called mongo but then you never call it.

Since you never call it, you never call res.render.

Even if you did call it, in the catch branch you never send an error response either.

It looks like you intended it to be an IIFE but forgot the () afterwards. There's no need for that though, you can just make the function you pass as the second argument to get() async instead.

router.route("/").get(async (req, res) => {
    const url = "mongodb://localhost:27017";
    const dbName = "Library";

    try {
        const client = await MongoClient.connect(url, { useUnifiedTopology: true });
        const db = client.db(dbName);
        const books = await db.collection("books").find().toArray();

        res.render({
            title: "Books",
            books,
        });
    } catch (err) {
        console.log(err);
        res.status(500).json({error: err})
    }  
});

Upvotes: 1

Bjørn Reemer
Bjørn Reemer

Reputation: 520

It seems you are not calling your async mongo function. You are only declaring your function. You need to call it by placing () after your declaration like so:

(async function mongo() {
    // Your code
})();

So applying that change to your code would result in the following:

const express = require("express");
const router = express.Router();
const { MongoClient } = require("mongodb");

/* GET Books page. */
router.route("/").get((req, res, next) => {
  const url = "mongodb://localhost:27017";
  const dbName = "Library";
  (async function mongo() {
    let client;
    try {
      client = await MongoClient.connect(url, { useUnifiedTopology: true });
      const db = client.db(dbName);
      const books = await db.collection("books").find().toArray();

      res.render({
        title: "Books",
        books,
      });
    } catch (err) {
      console.log(err);
    }
  })();
});


module.exports = router;

Because of this your request would indeed timeout as the code within the async function is not called.

Hope this answer was helpful 👍

Upvotes: 1

Related Questions