I am trying to insert data to Mongodb with the ajax $.post in the client side as the clident side using Cordova framework. But the data inserted to Mongo is empty, which is just has an ID. Below is the codes. Did I make any mistake? Could someone please help me figure it out? I am new to Mongo and ajax. Thanks!
codes in the client.js
$("#myPost").on("click", function () {
localData = localStorage.getItem('localData');
$.post("http://localhost:3000/postData",{localData:localData}, function (data, status) {
alert("Data received: " + data + "\nstatus: " + status);
});
});
codes in server.js
client.connect(err => {
currCollection = client.db("mydb").collection("db");
console.log ('Database connected!')
});
app.post('/postData', (req, res) => {
currCollection.insertOne(req.body, function(err,result){
if(err) {
console.log(err);
} else {
console.log("Data inserted: "+result);
}
})
});
The post result in actually [object Object] but I tried many ways still couldn't resolve the problem.