I'm trying to get data from my MongoDB database with nodejs/mongoose, then I send the data at a URL (http://localhost:3000/) and I want to get and manage the data in an HTML page. To get the data I make a GET request at 'http://localhost:3000/' with AJAX. I know it would be much easier using a view engine like ejs but this is a project for university and I have to use specifically AJAX. I have some problems sending the data with the API and also retrieving the data with AJAX. Here's my code.
This is the model for my Collection "Calendario" (the names are in Italian), this file is called calendario.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
//schema define the structure of the document
const calendarSchema = new Schema({
id_macchina: {
type: String,
required: true,
},
data_inizio: {
type: Date,
required: true,
},
data_fine: {
type: Date,
required: true,
},
noleggio: {
type: Boolean,
required: false,
},
restituzione: {
type: Boolean,
required: false,
},
});
module.exports = calendarSchema;
This is the file mongo.js where I connect to the database and I retrieve the data from the collection "Calendario" (I use '*' in the connection string for obvious reasons, in my code the connection string is correct). I already checked, and the connection works properly. Also, if I print "results" to the console (with console.log(results);) inside the function, Calendar.find(function (err, results) {}); I get the data I'm looking for in JS Object format. So until here everything seems to work in the right way.
const mongoose = require("mongoose");
const calendarSchema = require("./models/calendario");
const { MongoClient } = require("mongodb");
const mongouri =
"mongodb+srv://***:***@cluster0.joeep.mongodb.net/***";
var connection = mongoose.createConnection(mongouri);
var Calendar = connection.model("Calendario", calendarSchema, "Calendario");
exports.getCalendar = (req, res) => {
try {
Calendar.find(function (err, results) {
return results;
});
} catch (err) {
return err;
}
};
This is the file app.js where the is a simple API. I think here's the first problem, because if I try to print to the console, the output of mymongo.getCalendar() it prints "undefined".
const express = require("express");
const mymongo = require("./mongo.js");
const app = express();
app.listen(3000);
app.get("/", async function (req, res) {
res.send(await mymongo.getCalendar());
});
The second problem is in the HTML file called "noleggi.html". Here, I use AJAX to make a GET request to the address "http://localhost:3000/". In this code, I don't even try to show the data because it doesn't enter the function in the success field. In fact I put an alert and a console.log() inside the function and I can't see anything on the web page. Here's the code for this file:
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Calendar</title>
</head>
<body>
<div class="main class">
<h1>CALENDAR</h1>
<div class="calendar">
</div>
</div>
<script>
$.ajax({
type: 'GET',
data: JSON.stringify(data),
url: '/',
success: function (data) {
alert('test');
console.log('test');
}
});
</script>
</body>
</html>