gpt4 book ai didi

javascript - 引用错误: _ is not defined when using lodash to sort json attributes in node. js

转载 作者:行者123 更新时间:2023-11-28 18:59:29 26 4
gpt4 key购买 nike

我想对 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)

最佳答案

更改声明:

var underscore= require('下划线');

致:

var _ = require('下划线');

关于javascript - 引用错误: _ is not defined when using lodash to sort json attributes in node. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32893790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com