Database not connected

Hi all !

I have a project of create a MCQ app. I’m having trouble connecting to mongodb, and it’s preventing me from moving forward with my project. I found several sources on the internet that talked about removing autopublish, which I did, and I also followed the tutorials on publishing and subscribing.

Now, my application is unable to connect to a database (I changed the MONGO_URL variable). I changed the variable because I want to connect to an existing database.
I can guarantee you that the database exists, the collection exists and the data is filled in.

I’m on Windows 10 2004.

> echo  %MONGO_URL%
mongodb://localhost:27017/mydb

I probably make a mistake and I don’t see it, I’d just like to understand why I make that mistake so I don’t make it again.

My code (I followed the template with the meteor create --full command).

'/api/qcm/qcm.js'
import { Mongo } from 'meteor/mongo';
export const DB_QCM = new Mongo.Collection('qcm');

if (Meteor.isServer) {
    // This code only runs on the server
    Meteor.publish('qcm', function qcmPublication() {
        return DB_QCM.find();
    });
}

Meteor.methods({
    'qcm.insert'(title, url) {
        check(url, String);
        check(title, String);

        return DB_QCM.insert({
            url,
            title,
            createdAt: new Date(),
        });
    },
});

And as I saw somewhere, subscribing when the main page is loaded:

'startup/client/routes.js'
FlowRouter.route("/", {
  name: "App.home",
  action() {
    BlazeLayout.render("App_body", { main: "App_home" });
  },
  waitOn: function(){
    return Meteor.subscribe("qcm");
}
});

And the server register-api file :

'startup/server/register-api.js'
import '../../api/qcm/qcm.js';

On Chrome 83, here is my response when I search for DB_QCM :

image
The code using it :

'ui/pages/qcmpage/qcmpage.js'
import { Template } from 'meteor/templating';
import { DB_QCM } from "../../../api/qcm/qcm.js";

Template.QCM_Home.onCreated(function qcmHomeOnCreated() {
    this.state = new ReactiveDict();
    Meteor.subscribe('qcm');
});


Template.QCM_Home.helpers({
    qcms() {
        return DB_QCM.find();
    },
});

I searched everywhere but I haven’t found an answer that fits my problem.
What is wrong, and why ?

Nico

Hey Nico,
I don’t think this is a database error. The Meteor process wont start if it can’t connect to Mongo (it will throw a MongoNetworkError and exit)

The specific error QCM_DB is not defined, makes me think an import or export statement is incorrect.

Everything here looks correct, so double check that you don’t have any invisible characters in the string, and that you have saved your files (seriously, this happens to me a lot)

Also, one thing that would make the imports easier to read and verify, is to use absolute imports, ie. starting with “/
as an example, that means in 'ui/pages/qcmpage/qcmpage.js', instead of:
"../../../api/qcm/qcm.js"
you can import
"/api/qcm/qcm";

Another thing you can do is use the source panel of chrome’s inspector to set a breakpoint where the db is called and check what the value is at the time.

you can also use require in the browser console to check that an import path is working and exporting the correct module.

Hi @coagmano,
First, thank you so much for your attention !

I tried your tips but I didn’t really know what you were talking about :laughing: ! I’m a newbie sorry !

But I tried this :

  • I checked the syntax and there is no invisible characters
  • I debuged with meteor shell command and it appeared that Mongo had a value
    image
  • I also reviewed my code and added a page qcm to see what happens (I also added the correct route in routes.js file):
'qcm_page.html'
<template name="QCM_Home">
    {{> navbar}}
    <p>Debug</p>
</template>

'qcm_page.js'
import './qcm_page.html';
import '../../components/navbar/navbar.js';
import { Template } from 'meteor/templating';
import { DB_QCM } from "../../../api/qcm/qcm.js";


Template.QCM_Home.onCreated(function qcmHomeOnCreated() {
    this.state = new ReactiveDict();
    Meteor.subscribe('qcm');
});

According to the tutorial here, I’m supposed to be able to see my data.
And the page didn’t appeared. If I comment these lines, my page is rendering debug, like coded in the HTML file.

Template.QCM_Home.onCreated(function qcmHomeOnCreated() {
    this.state = new ReactiveDict();
    Meteor.subscribe('qcm');
});

What’s the hell? Does Meteor not like my qcmHomeOnCreated function?

Hi again,

I tried some new things that can clarify the situation. I followed this post on stackoverflow and the doc here.

As I could see, the callback function had no name. So I removed it in my code :

'api/qcm.js'
import { Mongo } from 'meteor/mongo';
export const DB_QCM = new Mongo.Collection('qcm');

if (Meteor.isServer) {
    // This code only runs on the server
    Meteor.publish('qcm', function() {
        return DB_QCM.find();
    });
}

And I did the same in the subscription file :

'qcm_page.js'
import "./qcm_page.html";
import "../../components/navbar/navbar.js";
import { Template } from "meteor/templating";
import { DB_QCM } from "../../../api/qcm/qcm.js";
import { Session } from 'meteor/session'

if (Meteor.isClient) {
  Meteor.startup(function () {
    Session.set("data_loaded", false);
  });

  Meteor.subscribe("qcm", function () {
    //Set the reactive session as true to indicate that the data have been loaded
    Session.set("data_loaded", true);
  });
}

if (Session.get("data_loaded")) {
  console.log("Data loaded");
  console.log("DB_QCM value :");
  console.log(DB_QCM);
}

As you can see, I print the value of DB_QCM in the console.
Here is the result:

I think the variable isn’t null, but when I write DB_QCM directly in the console, I have the same error as in my first post:

image

I’m really lost and I don’t really know what to do. Would someone be so kind as to explain to me what’s going on here?

EDIT:

I added autopublish package by meteor add autopublish and it works. Now, I really don’t understand where is my mistake. But I can work on my project.