错误:sort()只需要1个参数mongoose

时间:2014-03-23 12:45:40

标签: 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() 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()方法中,因为只需要1个Argument

2 个答案:

答案 0 :(得分:3)

您收到该错误,因为sort方法只接受一个参数,但您传递了两个参数。

所以请将您的电话改为:

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

或使用this answer中其他受支持的排序参数格式之一。

答案 1 :(得分:0)

从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) { ... });