Title: Express post request saving as undefined in DB
Tags: javascript;node.js;sqlite;express;body-parser
Question: I am learning how to set up a Express server and have run into an odd issue when creating new users.  The POST request goes through and gives no error and takes me to my dashboard but when I look in the sqlite DB it shows all values from the registration form saved as undefined.  I have installed body-parser and called it properly into the server, I believe.
I have tried changing extended: true instead of false and added adding res.setHeader('Content-Type', 'text/plain') into the app.use as stated by body-parser guide but it still saves as undefined every time.
here is my server.js code
const app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.setHeader('Content-Type', 'text/plain');
next();
here is the POST code in the server.js
app.post('/register', function(req, res) {
bcrypt.hash(req.body.password, saltRounds, function(err, hash) {
let db = new sqlite3.Database("./database/InvoiceApp.db");
let sql = `INSERT INTO users(name, email, company_name, password) VALUES ('${req.body.name}','${req.body.email}','${req.body.company_name}','${hash}')`;
db.run(sql, function(err) {
if (err) {
throw err;
} else {
return res.json({
status: true,
message: "User Created"
});
}
});
db.close()
and this is my form code, i am using vue.js
register () {
const formData = new FormData()
let valid = this.validate()
if (valid) {
formData.append('name', this.model.name)
formData.append('email', this.model.email)
formData.append('company_name', this.model.company_name)
formData.append('password', this.model.password)
this.loading = "Registering you, please wait"
axios.post('http://localhost:3128/register', formData).then(res =&gt; {
this.loading = ''
if (res.data.status === true) {
this.$router.push ({
name: 'dashboard',
params: { user: res.data.user }
})
} else {
this.status = res.data.message
}
})
} else {
alert('Passwords do not match')
}
I get no error messages when creating a new user, only if I try and login as a created user or do other actions linked to the created user.  Why is my form saving as undefined in the sqlite DB?
Comment: Make sure the data is being received at every level. Try doing `console.log(formData);` and make sure everything is good before your `axios.post()` function in your form code. Then, if it's good, check if it's being received correctly in the server with `console.log(req);`


Here is another answer: You should use JSON format from your Vue app instead of FormData.
FormData is an object that passes with Content-Type application/x-www-form-urlencoded.
And you body parser is configured for the Json.
Please try to replace
formData.append('name', this.model.name)
formData.append('email', this.model.email)
formData.append('company_name', this.model.company_name)
formData.append('password', this.model.password)
with an actual JS object
"name": this.model.name
.....other fields.....
and pass this object or this object stringified with JSON.strinfigy(obj);
Comment for this answer: thanks for the info @AndryShutka, spent a day learning about FormData and JSON and you were correct on sending the data in JSON format.  It was even easier than adding JS objects and using JSON.stringify().

Here is another answer: After spending a day researching FormData and JSON I figured it out thanks to this post and the answer by M U.  Because I was using Vue.js and had everything from the form stored using v-model all I had to do was update the post method to:
register () {
let valid = this.validate()
if (valid) {
this.loading = "Registering you, please wait"
axios.post('http://localhost:3128/register', this.model).then(res =&gt; {
this.loading = ''
if (res.data.status === true) {
this.$router.push ({
name: 'dashboard',
params: { user: res.data.user }
})
} else {
this.status = res.data.message
}
})
} else {
alert('Passwords do not match')
}
and the whole form uploaded to the database with the password hashed

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:06/19/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><node.js><sqlite><express><body-parser>', 'Title': 'Express post request saving as undefined in DB', 'ViewCount': 233, 'id': 56675399, 'user_AboutMe': '<p>Just starting out leaning coding.  Currently learning HTML, CSS and JavaScript.  No time like the present for a career change to something I enjoy</p>\n', 'user_AccountId': 15579083.0, 'user_DisplayName': 'jaronow', 'user_Reputation': 113.0, 'user_Views': 7.0, 'user_id': 11239440.0}}