Hello there,
I try to build an API REST with node.js (express, mongoose & MongoDB). I have imported a JSON files in my db, but It’s been couple days now that I didn’t manage to fetch any of these data. So here my code :
my index.js
const express = require('express');
const mongoose = require('mongoose');
const { ProcessModel } = require('./api/models/process');
const app = express()
const PROCESS = new ProcessModel()
mongoose.connect('mongodb://localhost/test-matrice', {
useNewUrlParser: true,
useUnifiedTopology: true
} )
.then(() => console.log('connected to mongoDb'))
.catch((err) => console.error('could not connect to mongoDb..', err));
app.use(express.json())
app.get('/api/all-process', (req, res) => {
console.log('test')
res.json(PROCESS.findAllProcess())
})
const port = 3003;
app.listen(port, () => console.log(`listening on the port ${port}`));
my ProcessModel :
const mongoose = require('mongoose');
const Process = mongoose.model('Process', new mongoose.Schema({
Lieux: String,
BusinesssUnit: String,
Activité: String,
Cycle: String,
Workflow: String,
....
}))
class ProcessModel {
constructor() {}
async findAllProcess() {
await Process.find();
}
}
module.exports.ProcessModel = ProcessModel;
my package.json :
{
"name": "test-matrice-mongo",
"version": "1.0.0",
"description": "mongodb testing import data",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"author": "thibault",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mongodb": "^4.1.2",
"mongoose": "^6.0.7"
},
"devDependencies": {
"nodemon": "^2.0.12"
}
}
When I test my route with insomnia it works fine, I have 200 status code, but I have no data in the response.
Can you help me please ?