简体   繁体   中英

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" }

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})
    }  
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM