开发者问题收集

在node.js中使用lodash对json属性进行排序时出现ReferenceError: _ is not defined

2015-10-01
10198

我想对我的 json 的属性进行排序。我读过一些关于使用 lodash 模块进行排序的方法。当我尝试根据价格(这是我的 json 的属性之一)进行排序时,我收到此错误:

server.js

var express= require('express');
var bodyParser= require('body-parser');
var morgan = require('morgan');
var config=require('./config');
var app= express();
var mongoose=require('mongoose');
var lodash= require('lodash');
var underscore= require('underscore');
//var User=require('./database/user')
mongoose.connect('mongodb://localhost:27017/db',function(err){
    if(err){
        console.log(err);
    }
    else{
        console.log("connected!");
    }
});
//res.json({message:"     " })

app.use(bodyParser.urlencoded({extended: true })); //if false then parse only strings
app.use(bodyParser.json());
app.use(morgan('dev'));//log all the requests to the console
var api=require('./app/routes/api')(app,express,underscore);
app.use('/api',api);
app.get('*',function(req,res){
    res.sendFile(__dirname + '/public/views/index.html');
});   // * means any route

app.listen(config.port,function(err){
    if(err){
        console.log(err);
    }
    else{
        console.log("The server is running");
    }
});
//module.exports = router;

api.js

var User= require('../models/user');
var Event=require('../models/event');
var config=require('../../config')

var secret=config.secretKey;

module.exports=function(app,express,underscore) {
    var api = express.Router();
    // app.use()

    api.post('/signup', function (req, res) {
        var user = new User({
            name: req.body.name,
            username: req.body.username,
            password: req.body.password
        });
        user.save(function (err) {
            if (err) {
                res.send(err);
                return;
            }
            res.json({
                message: 'User created!'
            });
        });

    });
    api.get('/users', function (req, res) {
        User.find({}, function (err, users) {
            if (err) {
                res.send(err);
                return;
            }
            res.json(users);
        });
    });

    api.post('/eventfeed', function (req, res) {
        var event = new Event({
            name: req.body.name,
            location: req.body.location,
            description: req.body.description,
            price: req.body.price,
            rating: req.body.rating
        });

        event.save(function (err) {
            if (err) {
                res.send(err);
                return;
            }
            res.json({
                message: 'Event created!'
            });
        });
    });
    api.get('/sortby_price',function(){
        var sorted = _.sortBy(Event, "price")
    });
    return api;
}

错误

 ReferenceError: _ is not defined
        at c:\Users\MY LAPY\WebstormProjects\Main\app\routes\api.js:62:22
        at Layer.handle [as handle_request] (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\layer.js:95:5)
        at next (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\route.js:131:13)
        at Route.dispatch (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\route.js:112:3)
        at Layer.handle [as handle_request] (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\layer.js:95:5)
        at c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:277:22
        at Function.process_params (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:330:12)
        at next (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:271:10)
        at Function.handle (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:176:3)
        at router (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:46:12)
        at Layer.handle [as handle_request] (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\layer.js:95:5)
        at trim_prefix (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:312:13)
        at c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:280:7
        at Function.process_params (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:330:12)
        at next (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:271:10)
        at logger (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\morgan\index.js:144:5)
    GET /api/sortby_price 500 12.021 ms - 1943
    GET /api/sortby_price 500 5.623 ms - 1943
    ReferenceError: _ is not defined
        at c:\Users\MY LAPY\WebstormProjects\Main\app\routes\api.js:62:22
        at Layer.handle [as handle_request] (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\layer.js:95:5)
        at next (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\route.js:131:13)
        at Route.dispatch (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\route.js:112:3)
        at Layer.handle [as handle_request] (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\layer.js:95:5)
        at c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:277:22
        at Function.process_params (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:330:12)
        at next (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:271:10)
        at Function.handle (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:176:3)
        at router (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:46:12)
        at Layer.handle [as handle_request] (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\layer.js:95:5)
        at trim_prefix (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:312:13)
        at c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:280:7
        at Function.process_params (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:330:12)
        at next (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\express\lib\router\index.js:271:10)
        at logger (c:\Users\MY LAPY\WebstormProjects\Main\node_modules\morgan\index.js:144:5)
2个回答

修改声明:

var underscore= require('underscore');

更改为:

var _ = require('underscore');

BrTkCa
2015-10-01
npm i underscore --save

var _ = require('underscore');
ASHISH R
2017-04-03