2014-02-06 3 views
0

mongoose-random 패키지를 사용하여 MongoDB 데이터베이스에서 임의의 항목을 가져옵니다. 그것은 내 mongoose-random 사용 것 같아몽구스 - 랜덤이 작동하지 않습니까?

// Bring Mongoose into the app 
var mongoose = require('mongoose'); 
var random = require('mongoose-random'); 

var DBNAME = 'test'; 
var COLLNAME = 'koleksigj'; 
var DBURI = 'mongodb://localhost/' + DBNAME; 

// Connect to DB 
mongoose.connect(DBURI); 

// Create the schema 
var quizSchema = new mongoose.Schema({ title: String, body: String }); 
quizSchema.plugin(random); 

// Make the model 
var Quiz = mongoose.model('Quiz', quizSchema); 

// Make query 
Quiz.findRandom(function (err, quiz) { 
    console.log(quiz); 
}) 

// CONNECTION EVENTS 
// When successfully connected 
mongoose.connection.on('connected', function() { 
    console.log('Mongoose default connection open to ' + dbURI); 
}); 

// If the connection throws an error 
mongoose.connection.on('error',function (err) { 
    console.log('Mongoose default connection error: ' + err); 
}); 

// When the connection is disconnected 
mongoose.connection.on('disconnected', function() { 
    console.log('Mongoose default connection disconnected'); 
}); 

// If the Node process ends, close the Mongoose connection 
process.on('SIGINT', function() { 
    mongoose.connection.close(function() { 
    console.log('Mongoose default connection disconnected through app termination'); 
    process.exit(0); 
    }); 
}); 

문서 here 유사하다 : 여기에 내 코드입니다. 당신이 NPM에보다 약간 다른 년대 Git repository의 예를 보면

hfz:mong-final Hfz$ node all.js 

/Users/Hfz/Dev/nodejs/mong-euro/mong-final/all.js:23 
Quiz.findRandom(function (err, quiz) { 
    ^
TypeError: Object function model(doc, fields, skipId) { 
    if (!(this instanceof model)) 
     return new model(doc, fields, skipId); 
    Model.call(this, doc, fields, skipId); 
    } has no method 'findRandom' 
    at Object.<anonymous> (/Users/Hfz/Dev/nodejs/mong-euro/mong-final/all.js:23:6) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Function.Module.runMain (module.js:497:10) 
    at startup (node.js:119:16) 
    at node.js:902:3 

답변

3

:

그러나이 오류 메시지가 표시됩니다.

quizSchema.plugin(random()); 
:

는 그것을 전달하는 대신 전화 random()을에 quizSchema에 플러그인을 추가하는 줄을 변경