1

Here's my js file describing the schema and API's that doesn't work. When I do this via the command line tools it does... The schema is pretty straightforward and I've implemented some simple find commands.

'use strict'

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

var validatePresenceOf = function(value){
  return value && value.length; 
};

var toLower = function(string){
  return string.toLowerCase();
};

var SportsStandings = new Schema({
  'sport' : { type : String, 
              validate : [validatePresenceOf, 'a sport is required'],
              set : toLower
            },
  'league' : { type : String, 
              validate : [validatePresenceOf, 'a league is required'],
              set : toLower
            },
  'division' : { type : String, 
              validate : [validatePresenceOf, 'a division is required'],
              set : toLower
            },
  'teamName' : { type : String, 
              validate : [validatePresenceOf, 'a teamName is required'],
              set : toLower
            },
   'wins' : { type : Number, min: 0, 
              validate : [validatePresenceOf, 'wins is required'],
            },
   'losses' : { type : Number, min: 0, 
              validate : [validatePresenceOf, 'losses is required'],
            }
});

SportsStandings.statics.findTeamRecord = function(sport, league, 
                                                  division, teamName,
                                              cb) {
  return  this.find({'sport' : sport, 'league' : league, 
                     'division' : division, 'teamName': teamName}, cb);
};

SportsStandings.statics.findBySport = function(sport, cb) {
  return  this.find({'sport' : sport}, cb);
};

module.exports = mongoose.model('SportsStanding' , SportsStandings);

and here's the simple node script that instantiates an object exported above and tries to do the save command on the Model.....

'use strict'

var util = require('util');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/mydb');
var SportsStanding  = require('../schemas/SportsStandings');

var record = new SportsStanding({ 
          'sport' : 'mlb',
          'league' : 'AL',
          'divison' : 'east',
          'teamName' : 'New York Yankees',
          'wins' : 10,
          'losses' : 1});

record.save(function(err) {
    console.log('error: ' + err);
    SportsStandings.find().all(function(arr) {
    console.log(arr); 
    console.log('length='+arr.length);
    });
});

process.exit();
2
  • What error are you seeing? When you say it doesn't work, is there some output that can help us diagnose the problem? Commented Apr 24, 2012 at 14:44
  • Nothing happens. I run the program using node on the command line: node sportsStandings.js and there's no error and nothing committed to the database so far as I can tell using the mongo command line tools. If I use the mongo command line to do db.sportsStandings.insert(....) it works.... Commented Apr 24, 2012 at 18:23

1 Answer 1

1

Please remember that when programming with node.js you have to be very careful with the event-driven programming style. The problem with your code appears to be that you're calling process.exit() at the outer execution level. When you call record.save(...) it will return control to that outer execution level immediately and not allow the save to execute or any of the code in the save's callback.

To solve this you'll want to move your process.exit() to the end of your inner-most callback function and then you should see the results you're expecting.

Running your example I found a couple of other typos and mistakes you'll need to correct. Check the spelling of your SportStanding(s) model variable and make sure it matches everywhere. Also, the find() on your model needs a callback and it returns all of the records in your database (as the second parameter - an error flag is the first) so there is no need for the chained all() call. What you want for your save function should end up looking like:

record.save(function(err) {
  console.log('error: ' + err);
  SportsStandings.find(function(err, arr) {
      console.log(arr);
      console.log('length='+arr.length);
      process.exit();
  });
});
Sign up to request clarification or add additional context in comments.

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.