1

I'm new to using MongoDB. Currently, I am making a web application that requires some data storage. I established an HTTP server on Node.js that runs on localhost:3000. I also built a virtual development environment using Vagrant and VirtualBox. I am accessing the Mongo shell from PuTTy (if that is relevant at all). Before incorporating MongoDB, it worked fine as I was storing the data in the Node.js program memory.

This web app is an online to-do-list. The first error that I am getting is with the get route. The list of "to-do"s that I have inserted into the Mongo database would not appear on the site in localhost. It seems to not be getting the data from the database. The second error that I am getting is with the post route. When I insert a "to-do" through the user interface at localhost and I refresh the page, the to-do-list on localhost gets updated with that particular to-do (but not with the ones I inserted in the database). But, it doesn't seem to add it into the database, and I still get this error on the console:

vagrant@precise32:~/app$ node server.js
{ description: 'descTest3', tags: [ 'tagTest3' ] }
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html 

I'm not sure why I got that error since I do not seem to be using any promises.

server.js

var express = require("express"), http = require("http"), app = express(), mongoose = require("mongoose");

app.use(express.static(__dirname + "/client"));

app.use(express.urlencoded());

mongoose.connect('mongodb://localhost/WatNext');//connect to the WatNext data store in mongoDB

var ToDoSchema = mongoose.Schema({ 
    "description": String,
    "tags": [ String ] //array of Strings
});

var ToDo = mongoose.model("ToDo", ToDoSchema)

http.createServer(app).listen(3000);
//get and post routes:

app.get("/toDos.json", function (req, res) {
    ToDo.find({}, function (err, toDos) {
        if (err != null){
            console.log(err);
        }
        res.json(toDos);
    });
});

app.post("/todos", function (req, res) {
    console.log(req.body);
    var addedToDo = new ToDo({"description" : req.body.description, "tags" : req.body.tags});
    //save function saves object into the database
    addedToDo.save(function (err, result) {
        if (err != null){//if there is an error
            console.log(err);
            res.send("ERROR SAVING");
        }
        else {
            ToDo.find({}, function (err, result) {
                if (err != null){//if there is an error in finding
                    res.send("ERROR FINDING");
                } 
                res.json(result);
            }); 
        }
    });
});

app.js

var main = function (toDoObjects) {
//do stuff with ToDoObjects including outputting the list of ToDos
};
$(document).ready(function() {
    $.getJSON("toDos.json", function(toDoObjects) {
        main(toDoObjects);
    })
});

mongo shell

> show dbs
WatNext 0.0625GB
local   0.03125GB
> use WatNext
switched to db WatNext
> show collections;
system.indexes
toDoCollection
todos
> db.toDoCollection.find();
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf1"), "description" : "Get groceries and eat afterwards", "tags" : [  "shopping",  "chores" ] }
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf2"), "description" : "Make up some new To-Dos", "tags" : [  "writing",  "work" ] }
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf3"), "description" : "Prep for Monday's class", "tags" : [  "work",  "teaching" ] }
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf4"), "description" : "Answer emails", "tags" : [  "work" ] }
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf5"), "description" : "Take April to the park", "tags" : [  "chores",  "pets" ] }
{ "_id" : ObjectId("58b38dd8fb355f57162d9cf6"), "description" : "Finish writing this book", "tags" : [  "writing",  "work" ] }

EDIT: I found out it was just an error of naming.

I also found out that

mongoose.Promise = require("bluebird");

solved the problem with the promise error. Remember to install the module first though:

npm install --save bluebird
Community
  • 1
  • 1
Steve D.
  • 316
  • 2
  • 6
  • 19
  • When you attempt the /post method from the UI, can you see the results through command line using db.toDoCollection.find() ? – Matt Feb 28 '17 at 02:21
  • When I do the post method from the UI, toDoCollection does not get updated. I type in db.toDoCollection.find(); but the only data that is listed are the ones that I inserted into the database through the shell myself. But the UI gets updated with the new data when I refresh though, for some reason. – Steve D. Feb 28 '17 at 02:59
  • Possible duplicate of [(node:3341) DeprecationWarning: Mongoose: mpromise](http://stackoverflow.com/questions/38138445/node3341-deprecationwarning-mongoose-mpromise) – Matt Feb 28 '17 at 17:58
  • I found a question that answers this. Immediately after you require mongoose, either call 'mongoose.Promise = global.Promise;' or import the 'bluebird' node package and call 'mongoose.Promise = require('bluebird');' BEFORE you make your connection. I have flagged this as a duplicate. Here is a link to the orginal answer http://stackoverflow.com/questions/38138445/node3341-deprecationwarning-mongoose-mpromise – Matt Feb 28 '17 at 18:02
  • that doesn't work either. – Steve D. Feb 28 '17 at 23:13

1 Answers1

1

I found out what was wrong. It was an error in naming for the GET and POST routes. It should have been:

app.get("/todos.json", function (req, res) { //get the data from the collection called 'todos' in MongoDB

as well as:

$.getJSON("todos.json", function(toDoObjects) {

I should have used the todos collection instead of toDoCollection:

 "description" : "descTest1", "_id" : ObjectId("58b39a1fb1a30305075408fa"), "tags" : [  "tagTest2" ], "__v" : 0 }
{ "description" : "descTest2", "_id" : ObjectId("58b4c837d47a5604c7c0609a"), "tags" : [  "tagsTest2" ], "__v" : 0 }
{ "description" : "descTest3", "_id" : ObjectId("58b4ca0491f4c804d200cda9"), "tags" : [  "tagTest3" ], "__v" : 0 }
{ "description" : "descTest4", "_id" : ObjectId("58b4e636b71d0a05ebb7a71a"), "tags" : [  "tagTest4" ], "__v" : 0 }
{ "description" : "descTest5", "_id" : ObjectId("58b60211428520053a4714ed"), "tags" : [  "tagTest5" ], "__v" : 0 }
{ "_id" : ObjectId("58b6037839e65d96e13cf68e"), "description" : "descTestMONGO", "tags" : [  "tagTestMONGO" ] }
{ "_id" : ObjectId("58b605b339e65d96e13cf690"), "description" : "Take April to the park", "tags" : [  "chores",  "pets" ] }
Steve D.
  • 316
  • 2
  • 6
  • 19