为什么没有输出?

时间:2015-10-05 21:46:54

标签: node.js

在我最近浏览过的内容中,我想列出我发布的事件的ID。但如果它超过五个,那么我只想要我发布的最后5个id。它没有显示任何错误,但没有给出任何结果。代码有什么问题吗?

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);
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");
    }
});

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) {
    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('/event', function (req, res) {
        Event.find({}, function (err, event) {
            if (err) {
                res.send(err);
                return;
            }
            res.json(event);
        });
    });
api.post('/recentlyviewed',function(req,res){
        User.findByIdAndUpdate({_id: req.body.userid},
            { $push:{"recent": {eventid: req.body.eventid},$each:[],$slice:-5}},
            { upsert :true},
            function(err,user){
                if(err) return err;
                else
                res.json(user);
            });
    });
    return api;
}

这是我的用户Scehma 的 event.js

var  UserSchema = new Schema({
    name: String,
    username: {type: String,required:true,index:{unique:true}},
    password: {type:String,required:true,select:false },
    favouriteid : [{eventid:String}],
    recent:[{eventid:String}]

1 个答案:

答案 0 :(得分:0)

findByIdAndUpdate只将id作为第一个参数而不是对象。检查docs您还错误地使用$ push和$ each组合,当他们应该成为"最近&#34的一部分时,您将分别提供$ each和$ slice ;对象和你要推送的对象应该在$ each数组中。它应该是

User.findByIdAndUpdate(req.body.id, {
        $push: {
            "recent": {
                $each: [ {eventid: req.body.eventid} ],
                $slice: -5
            }
        }
    },
    { upsert :true},//etc