0

I am new at MongoDB / Node.js / express. I'm setting up an app following this: https://www.youtube.com/watch?v=eB9Fq9I5ocs

I've created a db called "chatbot" and 2 collections within "chatbot": "countries" and "populations".

However, when I try to run the app to display the countries that I created in the shell, I get nothing but an empty array.

Here is the collection "countries":

> db.countries.find()
{ "_id" : ObjectId("58a0c3234d9ba38dcbe1769d"), "name" : "Afghanistan" }
{ "_id" : ObjectId("58a0c3844d9ba38dcbe1769e"), "name" : "Andorra" }

Here are my app files: package.json file:

{
  "name": "chatbot",
  "version": "1.0.0",
  "description": "chatbot app",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "express": "*",
    "body-parser": "*",
    "mongoose": "*"
  },
  "author": "Chris",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^1.11.0"
  }
}

app.js:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

Country = require('./models/country.js');

// Connect to Mongoose
mongoose.connect('mongodb://localhost/ChatbotService')
var db = mongoose.connection;

app.get('/', function(req, res){
    res.send('Hola');
});

app.get('/api/countries', function(req, res){
    Country.getCountries(function(err, countries){
        if(err){
            throw err;
        }
        res.json(countries);
    });
});

app.listen(8601);
console.log('Running on port 8601...');

country.js:

var mongoose = require('mongoose');

// Country Schema
var countrySchema = mongoose.Schema({
    name:{
        type:String,
        required: true
    },
    create_date:{
        type: Date,
        default: Date.now
    }
});

var Country = module.exports = mongoose.model('Country', countrySchema);

// Get Countries
module.exports.getCountries = function(callback, limit){
    Country.find(callback).limit(limit);
}

This is what I'm getting when I reload: result after reload Any help would be great.

2 Answers 2

1

Chatbot is your db name but here mongoose.connect('mongodb://localhost/ChatbotService') you are using chatbotservice. I think you are connecting with different db here.

Sign up to request clarification or add additional context in comments.

Comments

0

Specify the document name in the schema like:

var countrySchema = mongoose.Schema({
    name:{
        type:String,
        required: true
    },
    create_date:{
        type: Date,
        default: Date.now
    }
}, { **document name** });

Comments

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.