Search code examples
node.jstypescriptexpresshandlebars.jsexpress-handlebars

req.next() is not a function when rendering with express-handlebars


I am trying to render the login page of this web app.

It's made with TypeScript + node.js + express and express-handlebars

This is the code I am trying to run

import exphbs = require("express-handlebars");
import cookieParser = require("cookie-parser");
import express from "express";
import { cookieAuth } from "./auth";

const app = express();
        app.engine(
        "hbs",
        exphbs({
            extname: "hbs",
            layoutsDir: __dirname + "/views/pages/",
            partialsDir: __dirname + "/views/partials/"
        })
    );
    app.use(cookieParser());
    app.use(express.static("public"));
    app.set("view engine", "hbs");
    app.set("views", path.join(__dirname, "views"));
    
    app.get("/login", cookieAuth, (req, res) => {
        res.render("login");
    });
    
    app.get("/", cookieAuth, (req: any, res) => {
        if (req.asmuser == null) res.redirect("/login");
    });
    
    app.listen(3000);

Now when I try to visit / or /login I get this error:

    (node:9508) UnhandledPromiseRejectionWarning: TypeError: req.next is not a function
    at done (D:\antisocialmedianv2\node_modules\express\lib\response.js:1007:25)
    at ExpressHandlebars.renderView (D:\antisocialmedianv2\node_modules\express-handlebars\lib\express-handlebars.js:237:4)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9508) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9508) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I tried finding the problem myself but can't seem to get a solution.

I appreciate every bit of help.


Solution

  • In my cookieAuth function I called next() two times which caused this error.