嗨我想運行我的程序,但是當我嘗試在我的瀏覽器中連接到本地主機時,我每次都收到以下錯誤。我認爲我的排序或查詢有問題,但我不確定它究竟發生了什麼問題。任何人都可以幫助修復我的代碼app.js代碼也是正確的,我覺得那裏可能有錯誤..?任何幫助歡迎! :)Nodejs Mongoose - TypeError:無效排序()參數
Express
500 TypeError: Invalid sort() argument. Must be a string or object.
at Query.sort (C:\nodeapps\nodeblox\node_modules\mongoose\lib\query.js:1167:11)
at Function.Post.statics.getAll (C:\nodeapps\nodeblox\schemas\Post.js:44:9)
at module.exports.app.post.username (C:\nodeapps\nodeblox\routes\index.js:45:10)
at callbacks (C:\nodeapps\nodeblox\node_modules\express\lib\router\index.js:160:37)
at param (C:\nodeapps\nodeblox\node_modules\express\lib\router\index.js:134:11)
at pass (C:\nodeapps\nodeblox\node_modules\express\lib\router\index.js:141:5)
at Router._dispatch (C:\nodeapps\nodeblox\node_modules\express\lib\router\index.js:169:5)
at Object.router (C:\nodeapps\nodeblox\node_modules\express\lib\router\index.js:32:10)
at next (C:\nodeapps\nodeblox\node_modules\express\node_modules\connect\lib\proto.js:190:15)
at Object.methodOverride [as handle] (C:\nodeapps\nodeblox\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:37:5)
這裏是我的2級架構,我認爲是造成問題
Post.js
'use strict';
var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var validatePresenceOf = function(value){
return value && value.length;
};
var toLower = function(string){
return string.toLowerCase();
};
var getId = function(){
return new Date().getTime();
};
/**
* The Post schema. we will use timestamp as the unique key for each post
*/
var Post = new Schema({
'key' : {
unique : true,
type : Number,
default: getId
},
'subject' : { type : String,
validate : [validatePresenceOf, 'Subject is Required']
},
'content' : {type : String},
'author': String,
'tags' : {
type : String,
set : toLower
}
});
/**
* Get complete post details for all the posts
*/
Post.statics.getAll = function(cb){
var query = this.find({});
query.sort('key', -1);
return query.exec(cb);
};
/**
* Get only the meta information of all the posts.
*/
Post.statics.getAllMeta = function(cb){
return this.find({}, ['key','subject', 'author', 'tags'], cb);
};
Post.statics.findByKey = function(key, cb){
return this.find({'key' : key}, cb);
};
module.exports = mongoose.model('Post', Post);
user.js的
'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 getId = function(){
return new Date().getTime();
};
var User = new Schema({
'key' : {
unique : true,
type : Number,
default: getId
},
'username' : { type : String,
validate : [validatePresenceOf, 'a Username is required'],
set : toLower,
index : { unique : true }
},
'password' : String,
});
User.statics.findUser = function(username, cb){
return this.find({'username' : username}, cb);
};
User.statics.validateUser = function(username, password, cb){
this.find({'username' : username}, function(err, response){
var user = response[0];
if(!user || response.length === 0){
cb(new Error('AuthFailed : Username does not exist'));
}else{
if(password == user.password){
util.log('Authenticated User ' + username);
cb(null, user);
}else{
cb(new Error('AuthFailed : Invalid Password'));
}
}
});
};
module.exports = mongoose.model('User' , User);
最後我的應用程序。 js
'use strict'
/**
* Module dependencies.
*/
var express = require('express');
var util = require('util');
var Logger = require('devnull');
var logger = new Logger({namespacing : 0});
var mongoose = require('mongoose');
var http = require('http');
var app = express();
mongoose.connect('mongodb://localhost/testdb');
/**
* Application Configuration
*/
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.enable('jsonp callback');
app.set('view engine', 'jade');
app.set('view options', {layout : false});
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
secret : 'devadotD'
}));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(function(req, res, next){
res.locals.session = req.session;
next();
});
});
/**
* Application environment(s) Configuration
*/
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('stage', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
require('./routes')(app);
http.createServer(app).listen(app.get('port'), function(){
util.log("Express server listening on port " + app.get('port'), app.settings.env);
logger.log("Express server listening on port " + app.get('port'), app.settings.env);
});
module.exports = app;
有人幫忙嗎? – germainelol