resourcejs icon indicating copy to clipboard operation
resourcejs copied to clipboard

?limit and ?skip does not work?

Open johnsonice opened this issue 8 years ago • 6 comments

I followed the documentation to set server side code. Everything else works fine, but for some reason ?limit and ?skip in url does not work.

i am getting: { "status": 500, "message": "Failed to parse: { find: "cities", filter: {}, limit: "5" }. 'limit' field must be numeric.", "errors": {} }

any suggestions? here is all the dependency versions:

    "body-parser": "^1.15.0",
    "express": "^4.13.4",
    "lodash": "^4.5.0",
    "method-override": "^2.3.5",
    "mongoose": "^4.4.6",
    "resourcejs": "^1.0.6"

Here is my server side code

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var Resource = require('resourcejs');
var _ = require('lodash');

// Create the application.
var app = express();

// Add Middleware necessary for REST API's
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method-Override'));

app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  next();
});

// Connect to MongoDB
mongoose.connect('mongodb://localhost/hereseas_dev');
mongoose.connection.once('open', function() {
    // Load the models.
    app.models = require('./models/index');
    // Load the routes.
    var routes = require('./routes');

   Resource(app, '', 'resource', app.models.city).get().index();

  console.log('Listening on port 3000...');
  app.listen(3000); //set up http server, listen to port 3000
});

johnsonice avatar Mar 07 '16 06:03 johnsonice

@johnsonice can you provide example urls that you used, which didn't work?

zackurben avatar Mar 07 '16 14:03 zackurben

@zackurben thanks for your quick response

here is an example http://localhost:3000/resource?limit=99 i tried http://localhost:3000/resource?limit it will just retrieve everything in the collection. and this http://localhost:3000/resource?limit=-99 actually worked, returned 99 items. But i don't think it is how it should be.

no luck with ?skip at all.

I am very new to this. it could be I made a stupid mistake somewhere, but i just can't figure it out

cycz1235 avatar Mar 07 '16 21:03 cycz1235

@cycz1235 both skip and limit should work as follows:

example.com/resource?limit=5 example.com/resource?limit=5&skip=5

You can actually see them in the tests here: https://github.com/travist/resourcejs/blob/master/test/test.js#L688-L734

Just for background knowledge, what version of node are you running? I don't think this is an issue with ResourceJS, since the tests haven't complained, but maybe the version will tell us more.

Zack

zackurben avatar Mar 07 '16 21:03 zackurben

@zackurben i am running on node v5.4.0

johnsonice avatar Mar 07 '16 21:03 johnsonice

Did you ever get this handled? I was having the same issue. On node 6.9.1. Setting limit without a constraint returns just fine of course.

In looking into it I think that this has been fixed or at least the code is currently much different in the master than what I was using in my project. I think at issue is that your tutorial (maybe @johnsonice was using this? https://www.youtube.com/watch?v=OhPFgqHz68o&t=3775s) If they followed that or just used https://github.com/travist/meanapp/tree/resourcejs/server to get started, it calls for v0.1.25, and this version still suffers from the bug that was fixed on 4/5/16 62ff2a57437d1e626b72f3133cb30e1a84fbac80

I fixed it in that old version, in case anyone is unsure about the consequences of using the current version in their project by changing lines 323-324

.limit(parseInt(req.query.hasOwnProperty('limit') ? req.query.limit : pageRange.limit))
.skip(parseInt(req.query.hasOwnProperty('skip') ? req.query.skip : pageRange.skip))

Because for some reason it was not being considered an int.

Btw, @travist Thanks for the help in getting me started with my first REST API - I almost got excited that I had found and fixed a bug ;) for you.

prophet60091 avatar Dec 06 '16 17:12 prophet60091

Is there a place in the docs where the use of limit, skip etc... are mentioned? I see tests for them, but nothing mentioning their use.

claytongulick avatar Mar 24 '19 07:03 claytongulick