node.js - 错误: sort() only takes 1 Argument 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() 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 个参数

最佳答案

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

因此将您的调用更改为:

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

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

关于node.js - 错误: sort() only takes 1 Argument mongoose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22591096/

相关文章:

node.js - Mongoose 独特的属性仍然允许我保存到数据库

node.js - 如何使用特定集合Mongoose-Express-Mongodb (MEAN STACK)

node.js - AWS Lambda,API 网关返回格式错误的 Lambda 代理响应,502 错误

javascript - 如何在angular 2和expressjs nodejs中集成外部css和js

node.js - MongoDB 聚合和 $lookup 总是返回空数组

java - 从 mongodb 访问数据

node.js - Mongoose:如何将字段/值插入现有子文档中

javascript - 如何使用单个声明修改同时在服务器端和客户端使用的库?

javascript - npm 无法在日志上安装我的项目的依赖项 err 404

node.js - 如果文档的 _id 已经存在,如何将数组元素推送到数组;如果 _id 不存在,如何创建新文档?