1

I'm a total beginner at this and am using a tutorial to learn the basics of the MEAN stack. I am trying to return the documnents in my database to a web page but am instead receiving an empty array.

I have created a cluster on Mongodb Atlas called mytasklist. Inside here I created a database called mytasklistdb. Inside this I have a table (object) called mytasklistdb.mytasklisttutorial. My understanding of this is limited and so maybe I'm making a huge error somewhere here. I have experience of SQL but not Mongo and so the whole 'clusters' and 'collections' thing is new to me.

Anyway my code is as follows. I took the string for the database connection from the Mongo connection tab.

var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('mongodb+srv://myusername:[email protected]/test?retryWrites=true&w=majority', ['mytasklisttutorial']);

router.get('/tasks', function(req, res, next){
    db.mytasklistdb.find(function(err, tasks){
        if(err){
            res.send(err);
        }
        res.json(tasks);
    });
});

module.exports = router;

My database objects look like this:

_id:5db5f1f31c9d440000c3e7fe
title:"Walk the dog"    -   this is a string
isDone:false      -     this is boolean

I'm just getting an empty array but in the tutorial the guy is getting these 'documents'. What am I doing wrong?

EDIT: I realised that the 'tasks' part of the tutorial example was relating to a database called 'tasks'. Mine is called 'mytasklistdb'. I therefore changed this. I also added a parameter with the name of my collection to the line passed in to mongojs.

I have changed my code above to reflect this

7
  • can you share the video link and can you update question with your db schema ? Commented Oct 28, 2019 at 17:57
  • 1
    Do you have collection named tasks in the db. If yes you are not passing it as second parameter to the connection constructor. OR before the queries const collection = db.collection('tasks'); then do db.collection.find(...) Commented Oct 28, 2019 at 18:14
  • ambianBeing I have added these things - I think you were right but it still doesn't work Commented Oct 28, 2019 at 18:28
  • 2
    @user1480135 I think there is 1 more change that you need to do. the URL is connecting to a db called test which i suppose is not correct. Change the URL to something like: mongojs('mongodb+srv://myusername:[email protected]/mytasklistdb?retryWrites=true&w=majority', ['mytasklisttutorial']); If the db name is mytasklistdb and collection is mytasklisttutorial Commented Oct 28, 2019 at 18:35
  • 1
    Perfect that works thanks Commented Oct 28, 2019 at 19:16

2 Answers 2

1

The solution was to replace 'task' and 'test' with the name of my db. As follows:

var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('mongodb+srv://James:[email protected]/mytasklistdb?retryWrites=true&w=majority', ['mytasklisttutorial']);

router.get('/tasks', function(req, res, next){
    db.mytasklisttutorial.find(function(err, tasks){
        if(err){
            res.send(err);
        }
        res.json(tasks);
    });
});

module.exports = router;
Sign up to request clarification or add additional context in comments.

Comments

0

My guess is that you are not passing your query, just the callback in the find() method, probably you need to do something like this:

db.tasks.find({},function(err, tasks){
    if(err){
        res.send(err);
    }
    res.json(tasks);
});

1 Comment

It's still empty unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.