1

I am creating an express server in which I am trying to find the tracks in my database. Even though I have created the model to exactly how the attributes are in my database it still returns me an empty array. Please help

app.js

require('./config/config');
require('./db');
var Track = require('./models/track.model');

const mongoose = require('mongoose'),
      express = require('express'),
      bodyParser = require('body-parser');

var app = express();


const connection = mongoose.connection;

connection.once('open', () => {
      console.log('MongoDB database connection established successfully!');
});

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());

app.get('/', function(req, res) {
      Track.find({}, function(err, tracks) {
            if (!err) {
                console.log(tracks);
                process.exit();
            }
            else {
                throw err;
            }
        }); 
      res.sendFile('index.html', {root: __dirname});
});

app.listen(process.env.PORT, ()=> console.log(`Server started at port: ${process.env.PORT}`));

track.model.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

let Track = new Schema({
  Position: {
    type: Number
  },
  Track: {
    type: String
  },
  Artist: {
    type: String
  },
  Streams: {
    type: Number
  },
  Url: {
    type: String
  },
  Date: {
    type: String
  }
});

module.exports = mongoose.model('Track', Track);

Track Collection

2
  • 1
    Are you actually connecting to mongo database? mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true}); seems missing in your code. Commented Apr 8, 2020 at 6:16
  • yes I required db.js which reads const mongoose = require('mongoose'); mongoose.connect(process.env.MONGODB_URI, {useNewUrlParser: true}, (err) => { if (!err) { console.log('MongoDB connection succeeded.'); } else { console.log('Error in MongoDB connection : ' + JSON.stringify(err, undefined, 2)); } }); Commented Apr 8, 2020 at 6:19

2 Answers 2

2

You need to bind Schema to collection like this:

let Track = new Schema({
  Position: {
    type: Number
  },
  Track: {
    type: String
  },
  Artist: {
    type: String
  },
  Streams: {
    type: Number
  },
  Url: {
    type: String
  },
  Date: {
    type: String
  }
}, { collection : 'spotifyCharts' });
Sign up to request clarification or add additional context in comments.

2 Comments

still get an empty array
It should be { collection : 'spotifyCharts' }. Syntax error there.
0
if (!err){

     console.log(tracks);

     //Convert to JSON format

     res.json(tracks)

     process.exit();

   }

2.

Test along with Collection Name in module.exports

module.exports = mongoose.model('Track', Track, 'CollectionName')

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.