0

I am trying to get entries added to mongo db from heroku scheduler. But currently I am just testing using "node app/bin/trackStats"

Here is my model.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var StatsSchema = new Schema({
  // eMail address
  totalblc: { type: Number, required: false},
  date: { type: Date, default: Date.now },
  difficulty: {type: Number, unique: true},
  up: {type: String, required: false},
  // Name
});


module.exports = mongoose.model('Stats', StatsSchema);

Here is the code to add the enties #!/app/bin/node

var request = require('request');
var Stats = require('../models/stats');


function getStats() {
  request('http://someurl.com/', function (error, response, body) {
    if (!error) {
      console.log("tesT");
      console.log(body) // Print the google web page.
      var obj = JSON.parse(body);

      var coins = obj.coins;
      var difficulty = obj.difficulty;
      var up = obj.status;
      var stat = new Stats();
      stat.totalblc = coins;
      stat.difficulty = difficulty;
      stat.up = up;
      stat.save(function (err) {
        if (err) {
          console.log(err);


        }

        console.log("IN");
      });
     console.log("aa"); 
    }
    else {
      console.log(error);
      console.log("TEST");
    }
  });

}

getStats();

It console.logs all the data correctly, but nothing is added to the database.

h00j
  • 308
  • 1
  • 7
  • 18
  • Maybe you're looking in the wrong place. The docs will be in the `stats` collection, not `Stats`. – JohnnyHK May 12 '13 at 13:31
  • I believe I have found the source of the error, and that is that the database address is defined in /app/config/initializers/03_mongoose.js , and this isn't being read or noticed by my script. If I manually add the mongoose.connect in my script it works, but that messes up other pages due to trying to create two connections. So I am still stuck – h00j May 12 '13 at 16:47
  • Locomotive apps are usually started using the `lcm` command. That's not a strict necessity, you can also instantiate the Locomotive app manually, but it's still required before anything in `config/initializers` is executed. – robertklep May 12 '13 at 17:54

1 Answers1

0

Based upon your comment:

I believe I have found the source of the error, and that is that the database address is defined in /app/config/initializers/03_mongoose.js, and this isn't being read or noticed by my script. If I manually add the mongoose.connect in my script it works, but that messes up other pages due to trying to create two connections. So I am still stuck

...you have a problem I had when I was learning mongoose.

Here on StackOverflow, see:

  1. Defining Mongoose Models in Separate Module
  2. express: keep all DRY creating a mongoose connection module (using the new createConnection method)
  3. mongoose and model connections
Community
  • 1
  • 1
Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53