How can I know Meteor Mongo instance is connected to my MeteorJS application?


#1

I am currently following this tutorial: https://www.meteor.com/tutorials/react/collections However, after inputting document into database via meteor mongo my view is not updating. Additionally, Tasks.find({}).fetch(), returns an ampty array.

I suggest the database is not connected with my Meteor application. However, I do not know yet on how to check if there is database connection at all.

Here are my codes:

App.js

import React, { Component } from 'react';
import { withTracker } from 'meteor/react-meteor-data';

import { Tasks } from '../api/tasks.js';
import Task from './Task.js';

// Application Component
// This encapsulates the whole application.
class App extends Component {
    getTasks () {
        return [
            { _id: 1, text: 'This is task 1.' },
            { _id: 2, text: 'This is task 2.' },
            { _id: 3, text: 'This is task 3.' },
        ];
    }

    renderTasks () {
        return this.props.tasks.map((task) => (
          <Task key={task._id} task={task} />
        ));
    }

    render () {
        return (
            <div className='container'>
                <header>
                    <h1>To-do List Application</h1>
                </header>
                <ul>
                    { this.renderTasks() }
                </ul>
            </div>
        );
    }
}

export default withTracker(() => {
    return {
        tasks: Tasks.find({}).fetch(),
    };
})(App);

main.js

import React from 'react';
import { Meteor } from 'meteor/meteor';
import { render } from 'react-dom';

import '../imports/api/tasks.js'
import App from '../imports/ui/App.js';

Meteor.startup(() => {
  render(<App />, document.getElementById('render-target'));
});

#2

This bit is returning a predefined, static set of data:

    getTasks () {
        return [
            { _id: 1, text: 'This is task 1.' },
            { _id: 2, text: 'This is task 2.' },
            { _id: 3, text: 'This is task 3.' },
        ];
    }

Maybe you missed the edit to that code to return data from the database?


#3

It does not matter that method is not called anymore.


#4

I’m pretty sure meteor does not start unless the DB is connected, I’m sure I’ve had issues because the dev version of mongo was corrupted.


#5

Maybe your publications don’t send the docs to the client? If you have the autopublish package installed then this can’t be the cause of trouble.

Does Tasks.find({}).fetch() return nothing when inside the meteor mongo console or in the browser’s js console? Or both`


#6

Hey mate~

  • In Meteor Mongo console MeteorJS, with db.tasks.find();, returned the documents I have inputted.
  • However, in the client-side codes Tasks.find({}).fetch() returns an empty array.

#7

And what about the meteor shell console?


#8

Okay what commands should I execute there?

> Tasks.find({}).fetch()
ReferenceError: Tasks is not defined
    at evalCommandPromise.then (packages/shell-server/shell-server.js:246:21)
    at runBound (domain.js:314:12)
    at bound (domain.js:301:14)
    at defaultEval (repl.js:240:29)
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at repl:1:-61

#9

Oh, maybe you defined your collection only on the client? You need to have something like Tasks = new Mongo.Collection('tasks') in a server or common environment


#10

I am guessing you’re not subscribed to the data on the client.

Have you got a subscription?
An easy way to debug and check this would be to add meteor add autopublish. This will automatically push all the data to the client without subscription.

Now check to see if you have the data on the client. If you do, then you just need to fix your subscription.