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

Posted

技术标签:

【中文标题】错误:sort() 只需要 1 个参数 mongoose【英文标题】:Error: sort() only takes 1 Argument mongoose 【发布时间】:2014-04-30 17:29:57 【问题描述】:

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 个参数

【问题讨论】:

【参考方案1】:

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

所以把你的电话改成:

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

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

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于错误:sort() 只需要 1 个参数 mongoose的主要内容,如果未能解决你的问题,请参考以下文章

类型错误:generate_purchase_order() 只需要 1 个参数(给定 5 个)

c++sort函数“std::_Debug_range2”: 2 个重载中没有一个可以转换所有参数类型 错误

有啥方法可以克服mongo数据库中createview查询中的内存超出错误

宏传递了 2 个参数,但只需要 1 个

为啥聚合+排序比mongo中的查找+排序更快?

Pymongo 排序错误