I'm new to Node.js and SQLite, and now I'm trying to make a form to send data from the site and transfer it to the database, but I get the following error:
TypeError: Cannot read property 'ghostName' of undefined
at /home/user/server/index.js:23:29
at Layer.handle [as handle_request] (/home/user/server/node_modules/express/lib/router/layer.js:95:5)
at next (/home/user/server/node_modules/express/lib/router/route.js:137:13)
at /home/user/server/node_modules/body-parser/lib/read.js:130:5
at invokeCallback (/home/user/server/node_modules/raw-body/index.js:224:16)
at done (/home/user/server/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/home/user/server/node_modules/raw-body/index.js:273:7)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
And here is the code itself:
index.js
const express = require('express');
const Parser = require('body-parser');
const { request } = require('express');
const sqlite3 = require('sqlite3').verbose();
const server = express();
let path_services = ( __dirname + '/public/static/img/services/' );
let path_html = ( __dirname + '/public/index.html' );
let path_font = ( __dirname + '/public/static/font/' );
let path_css = ( __dirname + '/public/static/css/' );
let path_img = ( __dirname + '/public/static/img/' );
let path_js = ( __dirname + '/public/static/js/' );
let path_db = ( __dirname + '/data.db' );
const Body_Parser = Parser.urlencoded({extended: false});
server.get('/', Body_Parser , function(req, res) {
res.sendFile(path_html);
});
server.post('/', Body_Parser, function(req, res) {
let name = request.body.ghostName;
console.log(name);
let surname = request.body.ghostSurname;
let email = request.body.ghostEmail;
let phone = request.body.ghostPhone;
let message = request.body.ghostMessage;
let db = new sqlite3.Database(path_db, sqlite3.OPEN_READWRITE, (err) => {
if(err) {
console.error(err.message);
}
console.log('Connected to Database!');
});
db.run('INSERT INTO client(name) VALUES(?)', [name, surname, email, phone, message], function(err) {
if(err) {
console.log(err.message);
}
console.log('Good!');
});
db.close();
});
server.use(express.static (path_services) );
server.use(express.static (path_font) );
server.use(express.static (path_css) );
server.use(express.static (path_img) );
server.use(express.static (path_js) );
server.listen(8000);
Html-from:
<form action="/" method="post">
<input type="text" name="ghostName" class="contacts__name">
<input type="text" name="ghostSurname" class="contacts__surname">
<input type="email" name="ghostEmail" class="contacts__email">
<input type="text" name="ghostPhone" class="contacts__phone">
<input type="text" name="ghostMessage" class="contacts_message">
<input type="submit" value="" class="contacts_button">
</form>
I would be very grateful if someone explains the reason for the error. Now I'm trying to understand SQLite, but the process is moving slowly due to this error.
server.post('/', Body_Parser, function(req, res) { let name = request.body.ghostName;
You're accessing a variable named request
here (which does exist), but you should access req
, which is the request object associated with the request ("page load") by the user currently being handled.
request
is a variable outside of the route handler which doesn't change in your example and doesn't have a .body
attribute and even if it had, it wouldn't contain form values.
req
, one of the parameters of the function, is unique for every request (just like res
is) and because you've added a body parser, contains a .body
attribute (in addition to the regular .query
, .header
, etc).