你的jSON字符串应该包含哪些内容?

时间:2015-10-02 06:54:40

标签: node.js

我正在尝试根据价格对使用post方法创建的事件进行排序。搜索之后,我提出了一个解决方案,我不知道在json.parse中发送什么?我是新来的。帮我。这种方法是正确的还是有其他方法?

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,lodash);
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;

app.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,lodash) {
    var api = express.Router();
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('/event', function (req, res) {
        Event.find({}, function (err, event) {
            if (err) {
                res.send(err);
                return;
            }
            res.json(event);
        });
    });
 api.get('/sortby_price',function(req,res){

    Event.find({}, function (err, event) {
        if (err) {
            res.send(err);
            return;
        }
      res.json(event);
    });
        var o=JSON.parse(???????)
        var ascending=true;//change to false for descending
      o.sort(function(a,b) {
            return (a.price - b.price)*(ascending?1:-1);
        });

 //variable "o" now contains our sorted object.
        var json = JSON.stringify(o);//convert back to a string
//now do something with either the "o" object or "json" string
        res.json(json);
    });

        return api;

}

event.js

var mongoose= require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var  EventSchema = new Schema({
    name: String,
    location: String,
    description: String,
    price: Number,
    rating: Number
});
module.exports= mongoose.model('Event',EventSchema);
//module.exports = router;

1 个答案:

答案 0 :(得分:0)

不要在同一代码中解析和字符串化太多。 res.json()已经将对象字符串化,然后再将其发送给客户端。

您的 sortby_price 路线可以简化为:

api.get('/sortby_price', function (req, res) {
    Event.find({}, function (err, events) {
        if (err) {
            res.send(err);
            return;
        }

        var ascending = true;//change to false for descending
        events.sort(function (a, b) {
            return (a.price - b.price) * (ascending ? 1 : -1);
        });

        res.json(events);
    });
});