i try to create data with vue js, but backend can not read the data and just send “undefined” to database, i try create data with postman, backend can read the data,
i see the data captured on backend, it turns out that there is a difference between the data sent by vue js end the data sent by postman
here is the data sent by postman
[Object: null prototype] { name: '6', email: '5', password: '3' }
and here is the data sent by vue js
[Object: null prototype] {
'{"name":"2","email":"2","password":"2"}': ''
}
here is the script in vue js
<script>
import axios from "axios";
export default {
name: 'AddUser',
data(){
return {
model: {
name: '',
email: '',
id_status: '',
password: '',
}
};
},
methods: {
async saveUser(){
try{
const response = await axios.post('http://localhost:8000/users',this.model);
res.data.headers['Content-Type'];
console.log(response);
} catch (err){
console.log(err);
}
}
},
};
</script>
and here is script in node js as backend
if(q.pathname == "/users" && req.method == "POST"){
var body = '';
req.on('data', function(data){
body += data;
if(body.length > 1e6)
req.connection.destroy();
});
req.on('end', function(){
var postData = qs.parse(body);
let name = postData.name;
let email = postData.email;
let id_status = postData.id_status;
let password = postData.password;
let sql = `insert into users (name,email,id_status,password) values ('${name}','${email}','${id_status}','${password}')`
console.log(postData)
db.query(sql,(err, result) => {
if (err) throw err;
if(result.affectedRows == 1){
res.end(JSON.stringify({message: 'success'}));
}else{
res.end(JSON.stringify({message: 'failed'}));
}
});
});