2

嗨,我正在尝试运行我的程序,但是当我尝试在浏览器中连接到 localhost 时,每次都会出现以下错误。我认为我的排序或查询有问题,但我不确定它到底哪里出错了。任何人都可以帮助修复我的代码吗?app.js 代码是否也正确,我觉得那里也可能有错误..?欢迎任何帮助!:)

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

用户.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);

最后是我的 app.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; 
4

1 回答 1

6

您很可能使用的 mongoose 版本比您编写的代码要新。该.sort()方法已更新,现在它接受如下参数,用于降序:

query.sort('-key');

或者你可以使用这个版本:

query.sort({key: -1});

无论您使用哪个,您的都是过时的。请参阅最新文档

于 2012-11-23T20:51:32.993 回答