Fetching mongodb database imported files problem

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 ? :slight_smile:

I am not familiar with Mongdb and Node, but minor experience of API. The API-server must write to the body and you must read the body in some way. I use AJAX for the reading part.

var url = "https://api2.go4webdev.org/test/1";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.setRequestHeader("Accept", "application/json");

xhr.onload = function () {
   if (xhr.readyState === 4) {
      document.getElementById("put").innerHTML = this.responseText
   }};

xhr.send();

And to fetch data you must “allow” the AJAX to read by setting the correct headings in the API server. Otherwise you will be prohibited by the CORS policy (you must leave the door open on the API or use the right key).

 w.Header().Set("Access-Control-Allow-Origin", "*")
 w.Header().Set("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,CREATE,DELETE")
 w.Header().Set("Access-Control-Allow-Headers", "*")
 w.Header().Set("Content-Type", "application/json")

You can check if this is the case by examine the console in your browser.

@Sibert, thank you for your answer! I’m not quite sure, maybe I’m misunderstood something but, I’m actually creating an API REST, connected to a MongoDB. In this MongoDB, I have imported a JSON file from an Excel file to create a new collection in my database. My connected my server API to my Mongodb
So I don’t understand why should I fetch the data like it was an external API ?

Try to
return await Process.find()

in the ProcessModel

I assumed that it was a “common” REST API, which is an independent server. But REST is just a way to communicate.

As the API is normally a separate server, there are some CORS protections that you must deal with.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.