My goal is to send an array to my Node/MongoDB server using an AJAX POST request, along with other variables in the body. Check out the client side JS code below:
function setupForm(){
$("#add").submit(function(event) {
event.preventDefault();
var name = $("#name").val();
var logo = $("#logo").val();
var phone = $("#phone").val();
var email = $("#email").val();
var address = $("#address").val();
var postcode = $("#postcode").val();
var openingHours = $("#openingHours").val();
var loc = [44, 44];
$.ajax({
type: "POST",
url: "http://localhost:3000/services",
data: "name=" + name + "&logo=" + logo + "&phone=" + phone + "&email=" + email + "&address=" + address + "&postcode="+ postcode +"&openingHours=" + openingHours + "&loc=" + loc,
success: function(){alert('success');}
});
});
}
Here is the corresponding server side code:
exports.addWine = function(req, res) {
var wine = req.body;
console.log('Adding wine: ' + JSON.stringify(wine));
db.collection('services', function(err, collection) {
collection.insert(wine, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred'});
} else {
console.log('Success: ' + JSON.stringify(result[0]));
res.send(result[0]);
}
});
});
}
The current format of adding to the database is not what I desire. Can you help me achieve it in an array like this?
"postcode" : "ugh",
"openingHours" : "fhgfh",
"loc" : "44,44"