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

コードが作成されたバージョンよりも新しいバージョンのマングースを使用している可能性があります。.sort()メソッドが更新され、降順で次のようなパラメーターを受け取るようになりました。

query.sort('-key');

または、このバージョンを使用できます。

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

どちらを使用しても、あなたのものは時代遅れです。最新のドキュメントを参照してください。

于 2012-11-23T20:51:32.993 に答える