Node JS TypeError: Cannot read property ‘query’ of undefined - Solved

10-Sep-2022

.

Admin

Node JS TypeError: Cannot read property ‘query’ of undefined - Solved

Hi Dev,

I am going to show you an example of TypeError: Cannot read property ‘query’ of undefined. you can see TypeError: Cannot read property ‘query’ of undefined - solved. I’m going to show you about TypeError: Cannot read property ‘query’ of undefined in node js. In this article, we will implement a node js TypeError: Cannot read property ‘query’ of undefined.

I will provide a simple example of finding an error cannot read property ‘query’ of undefined. In this example use CRUD in the node js application.

So, let's start solve error:

Solve Error:


app.js

var app = express()

var mysql = require('mysql');

var connection = mysql.createConnection({

host : 'host_name',

user : 'user_name',

password : 'your_password',

database : 'database_name'

});

connection.connect();

app.post("/add", function(request, response, next){

name = request.body.name;

price = request.body.price;

detail = request.body.detail;

//insert query database

insertQuery = `INSERT INTO products (name,price,detail) VALUES ("${name}", "${price}", "${detail}")`;

// connetion database

connection.query(insertQuery, function(err, result) {

request.flash('success', 'Data added successfully!')

});

});

I hope it can help you...

#Node JS