내 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);
여기가
'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();
당신이보고있는 어떤 오류? 작동하지 않는다고 말하면 문제 진단에 도움이되는 결과물이 있습니까? –
아무 반응이 없습니다. 명령 줄에서 node : node sportsStandings.js를 사용하여 프로그램을 실행하면 mongo 명령 줄 도구를 사용하여 말할 수있는 한 오류가없고 데이터베이스에 커밋 된 것이 없습니다. mongo 명령 줄을 사용하여 db.sportsStandings.insert (....) 작업을 수행하면 ... – SPODOG