We are encountering an issue in Node.js with Mongoose where an instance of TypeError is being received instead of the required string type for the "path" argument

An Error Occurred Connection Successful, Port 4000 Listing Form node:internal/errors:464
ErrorCaptureStackTrace(err); ^

TypeError [ERR_INVALID_ARG_TYPE]: The argument for "path" must be a string. Instead, received a TypeError instance. at new NodeError (node:internal/errors:371:5) at validateString (node:internal/validators:119:11) at extname (node:path:837:5) at new View (D:\MERN\mernbackend\node_modules\express\lib\view.js:56:14) at Function.render (D:\MERN\mernbackend\node_modules\express\lib\application.js:570:12) at ServerResponse.render (D:\MERN\mernbackend\node_modules\express\lib\response.js:1012:7) at D:\MERN\mernbackend\src\app.js:30:13 at Layer.handle [as handle_request] (D:\MERN\mernbackend\node_modules\express\lib\router\layer.js:95:5) at next (D:\MERN\mernbackend\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (D:\MERN\mernbackend\node_modules\express\lib\router\route.js:112:3) { code: 'ERR_INVALID_ARG_TYPE' }

This is the app.js File

const path = require('path');
const hbs = require('hbs');
const app = express();
require('./db/connect');
const port = process.env.PORT || 4000;

const templatePath = path.join(__dirname, '../templates/views')
const partialsPath = path.join(__dirname, '../templates/partials')
app.set('view engine', 'hbs');
app.set('views', templatePath)
hbs.registerPartials(partialsPath);

app.get('/', (req, res) => {
    res.render('index')
})

app.get('/register', (req, res) => {
    res.render('register')
})

app.get('/login', (req, res) => {
    res.render('login')
})

app.post('/register', async (req, res) => {
    try {
        console.log(req.body.fname);
    } catch (err) {
        res.render(err)
    }
})

app.listen(port, (err) => {
    console.log(`Listing from Port ${port}`);
})

**This is Register.hbs**
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{{#headLink}}
<title>Register</title>
</head>
<body>

<style>
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}

/* Firefox */
input[type=number] {
-moz-appearance: textfield;
}
</style>
{{#nav}}
<form action="/register" method="POST">  

<label> First Name </label>         
<input type="text" name="firstname" size="15"/> <br /> <br />  
<label> Middle Name: </label>     
<input type="text" name="middlename" size="15"/> <br /> <br />  
<label> Last Name: </label>         
<input type="text" name="lastname" size="15"/> <br /> <br />  

<label>   
Course :  
</label>   
<select>  
<option value="Course">Course</option>  
<option value="BCA">BCA</option>   
<option value="BBA">BBA</option>  
<option value="B.Tech">B.Tech</option>  
<option value="MBA">MBA</option>  
<option value="MCA">MCA</option>  
<option value="M.Tech">M.Tech</option>  
</select>  

<br>  
<br>  
<label>   
Gender :  
</label><br>  
<input type="radio" name="gender"/> Male <br>  
<input type="radio" name="gender"/> Female <br>  
<input type="radio" name="gender"/> Other  
<br>  
<br>  

<label>   
Phone :  
</label>  
<input type="text" name="country code"  value="+91" size="2"/>   
<input type="number" name="phone" size="10"/> <br /> <br />  
Address  
<br />  
<textarea cols="80" rows="5" value="address" name="address">  
</textarea>  
<br /> <br>  
Email:  
<input type='email' id='email' name='email'/> <br>    
<br> <br>  
Password:  
<input type='password' id='pass' name='pass'> <br>   
<br> <br>  
Re-type password:  
<input type='password' id='repass' name='repass'> <br> <br>  
<input type='submit' value='register'/>  
</form> 
</body>
</html> 

Answer №1

To improve your code, consider inserting a / in the second argument on line 7:

const templatePath = path.join(__dirname, '/../templates/views')

You might also want to do something similar for line number 8.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing a notification system similar to Stack Overflow in a Node.js and MongoDB application

Apologies if my inquiry seems vague, but let me explain my situation. I have a forum similar to StackOverflow, built on nodejs and mongodb. The forum includes posts and comments. I am in need of implementing a notification system that alerts users when a n ...

What is the best approach to transfer information from the client side to a node

I have an ejs page that looks like this: <%- include('../blocks/header', {bot, user, path}) %> <div class="row" style="min-width: 400px;"> <div class="col" style="min-width: 400px;"> <div class="card text-white mb-3" & ...

Transfer the AWS configuration settings to the imported module

Currently, I am attempting to perform unit testing on a JS AWS Lambda by running it locally. To simulate the Lambda environment, I am taking on the same role that the Lambda would have with AWS.config.credentials and then executing the Lambda function that ...

Harness the power of the ioHook Node.js global native keyboard and mouse listener within your Browser environment

I'm dealing with a challenging issue that seems to have no solution due to security limitations. However, I'm reaching out to you as my last hope to find a workaround. For my project, I require a system that can monitor user mouse and keyboard a ...

What is the best way to activate a post function using a hyperlink?

I'm struggling to figure out how to call my post function using a link within my navbar. The issue is that I'm working with links in the navbar code, and not sure how to integrate calling the post function with them. Below is the code for my pos ...

Issue encountered while executing tasks in the Gruntfile.js file

Having trouble with Grunt concatenating my CSS files into one named production.css Below is the output I received from the command prompt: C:\Users\josha\Desktop\Repos\AJAX Project - Grunt Test>grunt C:\Users\josha& ...

Updating the _id of a Mongoose document upon save or update

Just a quick question: I noticed that when I push an update, Mongoose is changing/upgrading the _id field of a document. Is this behavior intentional? Thank you. Here's the update code I'm using in my PUT route. It successfully returns the upd ...

Installing NPM modules globally requires the use of sudo to do so successfully

After reinstalling Ubuntu 12.04 LTS, I took the following steps: Firstly, I installed Node using the package manager with the provided script below: sudo apt-get update sudo apt-get install python-software-properties python g++ make sudo add-apt-rep ...

How to remove various items from Google Cloud Storage with node.js

I'm looking to mass delete multiple objects from Google Cloud Storage. Currently, I've been deleting one object at a time using the following code: var gcloud = require('gcloud')({ projectId: "sampleProject1" }); var gcs = gcloud.sto ...

The Socket.io Namespace does not support the adaptor method

I am currently working on an application where I need to create dynamic namespaces. Whenever a new namespace is created, I attach a redis-adapter to it for scalability reasons. However, when implementing this process, I encounter the following error: var ...

The configuration settings for Express are not functioning properly

Initially, I utilized the following code: app.configure( function(){ app.use(express.static(__dirname, '/')); }); Later, I realized that it was an outdated version. So, I switched to: var env = process.env.NODE_ENV || 'development&apo ...

Angular 2 - Can a Content Management System Automate Single Page Application Routing?

I am interested in creating a single-page application with an integrated content management system that allows users to edit all aspects of the site and add new pages. However, I have found it challenging to configure the SPA to automatically route to a n ...

Unlocking the Power of Deployd: A Comprehensive Guide to Harnessing dpd-passport for Safe

Firstly, I must express my appreciation for Deployd. I am eager to utilize it in a production environment; however, I require OAuth and social logins integration. To achieve this, I have installed the dpd-passport module. Although it functions admirably, I ...

An array containing numerous "case" triggers

var message = "hello [[xxx]] bye [[ZZZ]]" var result, re = /\[\[(.*?)\]\]/g; while ((result = re.exec(message)) != null) { switch (result[1].toLowerCase()) { case "xxx": console.log("found xxx"); br ...

Using a nodejs module is causing an error in my code

I am dealing with a module like this: module Net.Server { var socket:dgram.Socket; [...] } Here is my app.ts file: var server:Net.Server = new Server(); However, when I include this line at the beginning of the first file: import dgram = requ ...

Contrary to expectations, the middleware EJS fails to render JPG files at

I am currently working on a NodeJS server using EJS. The goal of this server is to render an HTML page that contains a line of text and a jpg file. However, I am encountering an issue with the jpg file not being loaded by the server. Even though I have sp ...

The installation of npm fails due to a conflict with the peer dependency version of [email protected]

Encountered an issue while attempting to install dependencies for this project, where the command npm install failed with the following error: > npm install npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: & ...

Utilize the request module to fetch JSON data and then deliver the desired output

Utilizing the request module for handling HTTP requests in Node.js has proven to be quite handy. Here's a snippet of code showcasing its usage: module.exports.getToken = function(){ var token ; request(validLoginRequest, function(err,resp,b ...

Guide to launching Cardano Wallet

After following the steps in this guide, I have successfully installed cardano-wallet. However, I am unsure how to run it and interact with it using node js: const { WalletServer } = require('cardano-wallet-js'); let walletServer = WalletServer. ...

Leveraging Shared Functions Across Various Routes in Express with Node.js

Is there a way to implement shared methods/functions that can be utilized by various routes created in express within nodejs? I have developed a nodejs server which currently supports 3 APIs. For instance: /cars /cars/:id /cars/:id/sellers All these 3 A ...