Friday 25 November 2016

Express w/ Mongoose: Can't increment beyond 5 or so increments

Essential functionality here is 1 click = 1 vote, it updates a vote parameter in MongoDB and it is incrementing within MongoDB just fine. However after about 5 votes it stops incrementing. When I refresh the browser it allows me 5 more increments then no more until the next refresh..Is there something I'm missing, perhaps pertaining to the 'findOneAndUpdate' method? I've tried just using the 'update' method and the behavior is the same. I've checked the docs and found no mention of a '$inc' increment/time/interval limit. Here is my server.js: var express = require('express'); var mongoose = require('mongoose'); var Schema = mongoose.Schema; var bodyParser = require('body-parser'); var app = express(); mongoose.connect('mongodb://localhost:27017/food'); //Allow all requests from all domains & localhost app.all('/*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Methods", "POST, GET"); next(); }); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false})); var usersSchema = new Schema({ id: String, vote: Number }); var bkyes = mongoose.model('bkyes', usersSchema); app.get('/bkyes', function(req, res) { bkyes.find({}, function(err, bkyes) { res.send(bkyes); }); }); app.post('/bkyes', function(req, res, next) { bkyes.findOneAndUpdate({$inc: { vote: 1 }}) .exec(function(err, foundObject) { if (err) { console.log(err); }}); }); app.listen(3000);

Submitted November 26, 2016 at 05:25AM by joWebDev

No comments:

Post a Comment