2012-04-24 11 views
1

これは私のjsファイルで、動作しないスキーマとAPIを記述したものです。コマンドラインツールでこれを行うと、スキーマはかなり簡単になり、簡単なfindコマンドを実装しました。mongooseを使用してノードスクリプト経由でmongodbレコードを保存できません

'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); 

、ここで上記のエクスポートされたオブジェクトをインスタンス化し、モデルにsaveコマンドを実行するためにしようと、単純なノードのスクリプトです.....

'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(); 
+0

どのようなエラーが表示されますか?それがうまくいかないと言うと、私たちが問題を診断するのに役立つ出力がありますか? –

+0

何も起こりません。コマンドラインのnode:node sportsStandings.jsを使用してプログラムを実行します。mongoコマンドラインツールを使用して、これまでのところデータベースにコミットされたエラーも何もありません。 mongoコマンドラインを使用してdb.sportsStandings.insert(....)を実行すると、それは機能します。 – SPODOG

答えて

1

でプログラミングするときことを覚えておいてくださいイベント駆動型のプログラミングスタイルには非常に注意する必要があります。あなたのコードの問題は、外部実行レベルでprocess.exit()と呼んでいるようです。 record.save(...)を呼び出すと、即座にその外部実行レベルに制御が戻され、保存が実行されたり、コールバックのコードが実行されたりすることはありません。

これを解決するには、最も内側のコールバック関数の最後にprocess.exit()を移動して、期待している結果が表示されるはずです。

あなたの例を実行する私は、修正する必要があるいくつかの他のタイプミスと間違いを発見しました。 SportStanding(s)モデル変数のスペルを確認し、どこでも一致することを確認してください。また、お使いのモデルのfind()は、コールバックを必要とし、それはあなたのデータベース内のすべてのレコードを返します(2番目のパラメータとして - エラーフラグが最初である)ので、チェーンall()通話のための必要はありません。保存機能を使いたい場合は、次のようになります。

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

ありがとう、それはそれを修正しました。 – SPODOG

関連する問題