Hello, I have created some JSON documents and populated them with data. One of these documents is named Biens.json. When I execute db.Biens.find()
on the MongoDB shell, it successfully retrieves and displays the data. However, when I attempt to retrieve the same data using a RESTful approach, nothing is displayed; only an empty array []
is returned. I am using a Mac, and I have already installed and imported Express and MongoDB. To run the code, I use the command node serveur.js
.
Here is my code : `var express = require(“express”);
var app = express();
app.use(express.json());
app.listen(8888);
async function main() {
var MongoClient = require(“mongodb”).MongoClient;
var url = “mongodb://localhost:27017”;
const client = new MongoClient(url);
const connexion = await client.connect();
console.log(‘Connecté à la base de données’);
db = connexion.db(“MEAN”);
app.get("/Biens", async (req, res) => {
console.log("/Biens");
let nbDocuments = await db.collection("Biens").countDocuments();
console.log(nbDocuments);
let documents = await db.collection("Biens").find().toArray();
res.json(documents);
});
}
main();
`