javascript Nodejs Mongoose - TypeError:无效的sort()参数

uurv41yg  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(114)

你好,我正在尝试运行我的程序,但当我尝试连接到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)

字符串
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);

dfddblmv

dfddblmv1#

你很可能使用了比你的代码更新的mongoose版本。.sort()方法已经更新,它现在接受这样的参数,按降序排列:
query.sort('-key');
或者你可以使用这个版本:
query.sort({key: -1});
无论你用的是什么,你的都已经过时了。查看最新文档。

相关问题