I am new to mongodb, I am using Express, Body-Parser and Mongoose and I have a mongodb database (its online, its call mlab) where i pull data from. Every thing is working fine, i have used postman to Get, Post and Delete. I am trying to get the data in JavaScript so i can display in the html. I am using Ajax, it works and returns success, but fires the fail function of the Ajax.
The mongdb controller
exports.photoalbum_all = function (req, res, next) {
PhotoAlbum.find(({}), function (err, photoalbum) {
if (err) return next(err);
res.send("../Views/images", {photo: photoalbum});
});};
The Ajax call I'm making
function getPhoto() {
var photoAdd = 'http://localhost:1234/photoalbums/find';
$.ajax({
header: 'Access-Control-Allow-Origin: *',
dataType: "jsonp",
url: photoAdd,
contentType: 'application/json; charset=utf-8',
async: false,
crossDomain: true,
success: AjaxSucceeded,//callback,
error: AjaxFailed
});
}
function AjaxSucceeded(result) {
alert("hello");
alert(result);
}
function AjaxFailed(result) {
debugger;
alert("hello1");
alert(result.statusText);
}
getPhoto();
At first i got errors about ajax cross-origin request blocked so i had to use datatype: 'jsonp'. Like i said, when i check in the network of the chrome browser console, i get the success and this address:
http://localhost:1234/photoalbums/find?callback=jQuery111202567313069615542_1545928466705&_=1545928466706
which returns this Json values
[{"_id":"5c22a5ffcb29611f905f756b","title":"prayer","albums":[{"_id":"5c22a5ffcb29611f905f756c","u_name":"family01","u_title":"family_man"}],"createdAt":"2018-12-25T21:49:51.091Z","updatedAt":"2018-12-25T21:49:51.091Z","__v":0},{"_id":"5c22a66bd3527c39342fafe7","title":"prayer","albums":[{"_id":"5c22a66bd3527c39342fafe8","u_name":"family01","u_title":"family_man"}],"createdAt":"2018-12-25T21:51:39.274Z","updatedAt":"2018-12-25T21:51:39.274Z","__v":0}]
i have exhausted myself researching but to no avail, i am suspicious though, could the Json response i'm getting from mlab be the problem, i mean the formatting. I am open to any ideas. Thanks for your help in advance.