Node.js 错误:sort()只接受1个参数mongoose

Node.js 错误:sort()只接受1个参数mongoose,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,node.js和mongodb应用程序给了我一个错误,我将在下面描述 C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog>node app.js connect.multipart() will be removed in connect 3.0 visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives connect.limit() w

node.js和mongodb应用程序给了我一个错误,我将在下面描述

C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog>node app.js
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
Express server listening on port 8082
sort() only takes 1 Argument
Error: sort() only takes 1 Argument
    at Query.sort (C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\node_modules\mongoose\lib\query.js:1306:11)
    at PostsDAO.getPosts (C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\posts.js:49:21)
    at ContentHandler.displayMainPage (C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\routes\content.js:13:15)
    at callbacks (C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\node_modules\express\lib\router\index.js:164:37)
    at param (C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\node_modules\express\lib\router\index.js:138:11)
    at pass (C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\node_modules\express\lib\router\index.js:145:5)
    at Router._dispatch (C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\node_modules\express\lib\router\index.js:173:5)
    at Object.router (C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\node_modules\express\lib\router\index.js:33:10)
    at next (C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\node_modules\express\node_modules\connect\lib\proto.js:193:15)
    at C:\Node.js\M101JS\WEEK4\hw4-3.1ab4521760e8\hw4-3\blog\routes\session.js:19:20
app.js服务器的代码是

var express = require('express')
  , app = express()  
  , cons = require('consolidate')  
  ,mongoose = require('mongoose')
  , routes = require('./routes'); 
mongoose.connect('mongodb://localhost:27017/dbs');
var dbs = mongoose.connection;
dbs.on('error', console.error.bind(console, 'connection error:'));
dbs.once('open',  function(err, db) {
    "use strict";
    if(err) throw err;
    app.engine('html', cons.swig);
    app.set('view engine', 'html');
    app.set('views', __dirname + '/views');
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    routes(app, db);
    app.listen(8082);
    console.log('Express server listening on port 8082');
});
而post.js代码是

var Post = require('./models-new/post.js');

function PostsDAO(db) {
    "use strict";

    if (false === (this instanceof PostsDAO)) {
        console.log('Warning: PostsDAO constructor called without "new" operator');
        return new PostsDAO(db);
    }

    //var posts = db.collection("posts");

    this.insertEntry = function (title, body, tags, author, callback) {
        "use strict";
        console.log("inserting blog entry" + title + body);

        var permalink = title.replace( /\s/g, '_' );
        permalink = permalink.replace( /\W/g, '' );

        var newPost = new Post ({"title": title,
                                "author": author,
                                "body": body,
                                "permalink":permalink,
                                "tags": tags,
                                "comments": [],
                                "date": new Date()});


        Post.save(post, function (err, result) {
            "use strict";

            if (err) return callback(err, null);

            console.log("Inserted new post");
            callback(err, permalink);
        });

    }


    this.getPosts = function(num, callback) {
        "use strict";

        Post.find().sort('date', -1).limit(num).toArray(function(err, items) {
            "use strict";

            if (err) return callback(err, null);

            console.log("Found " + items.length + " posts");

         callback(err, items);
        });
    }

    this.getPostsByTag = function(tag, num, callback) {
        "use strict";

        Post.find({ tags : tag }).sort('date', -1).limit(num).toArray(function(err, items) {
            "use strict";

           if (err) return callback(err, null);

            console.log("Found " + items.length + " posts");

          callback(err, items);
       });
    }

    this.getPostByPermalink = function(permalink, callback) {
        "use strict";
        Post.findOne({'permalink': permalink}, function(err, poste) {
            "use strict";

            if (err) return callback(err, null);

            callback(err, poste);
        });
    }

    this.addComment = function(permalink, name, email, body, callback) {
        "use strict";

        var comment = {'author': name, 'body': body}

        if (email != "") {
            comment['email'] = email
        }

        Post.update({'permalink': permalink}, {'$push': {'comments': comment}}, function(err, numModified) {
            "use strict";

            if (err) return callback(err, null);

            callback(err, numModified);
        });
    }

}

module.exports.PostsDAO = PostsDAO;
我不知道当环境给了我错误时,我能做些什么来解决这个问题
在sort method becouse中,只接受1个参数

您会得到该错误,因为该方法只接受一个参数,但您要传递两个参数

因此,请将您的通话改为:

Post.find().sort('-date').limit(num).toArray(function(err, items) {

或者使用其他受支持的排序参数格式。

由于该方法只接受一个参数,而传递了两个参数,因此会出现此错误

因此,请将您的通话改为:

Post.find().sort('-date').limit(num).toArray(function(err, items) {

或者使用其他受支持的排序参数格式。从4.x开始,排序方法已更改。如果您使用的是>4.x。尝试使用以下任何一种方法

Post.find({}).sort('-date').exec(function(err, docs) { ... });
Post.find({}).sort({date: -1}).exec(function(err, docs) { ... });
Post.find({}).sort({date: 'desc'}).exec(function(err, docs) { ... });
Post.find({}).sort({date: 'descending'}).exec(function(err, docs) { ... });
Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
Post.find({}, null, {sort: '-date'}, function(err, docs) { ... });
Post.find({}, null, {sort: {date: -1}}, function(err, docs) { ... });

从4.x开始,排序方法已更改。如果您使用的是>4.x。尝试使用以下任何一种方法

Post.find({}).sort('-date').exec(function(err, docs) { ... });
Post.find({}).sort({date: -1}).exec(function(err, docs) { ... });
Post.find({}).sort({date: 'desc'}).exec(function(err, docs) { ... });
Post.find({}).sort({date: 'descending'}).exec(function(err, docs) { ... });
Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
Post.find({}, null, {sort: '-date'}, function(err, docs) { ... });
Post.find({}, null, {sort: {date: -1}}, function(err, docs) { ... });