Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Criteria is not accessible, when :id is invalid #88

Closed
f1nnix opened this issue Aug 14, 2015 · 2 comments
Closed

Criteria is not accessible, when :id is invalid #88

f1nnix opened this issue Aug 14, 2015 · 2 comments

Comments

@f1nnix
Copy link

f1nnix commented Aug 14, 2015

Refrenced to #85

My API provides aliases for id. /users/me/ === /users/5, if current user's ID is 5. So, I've tried such approach:

users.read.fetch.before(function(req, res, context) {
  var userId = req.params.user_id

  if (userId === "me") {
    context.criteria.id = req.user.id
  }
  return context.continue;
})

Not working. Any ideas?

  1. this should work, can you please open a new issue so we can discuss this if there is an actual problem here.
var milestones = {
  users: require('./users.js')
}

var users = epilogue.resource({
  model: db.User,
  associations: true
});

users.use(milestones.users)
// ./users.js
module.exports = {
  read: {
    start: function(req, res, context) {
      console.log(context);
      return context.continue;
    },
    auth: function(req, res, context) {
      console.log(context);
      return context.continue;
    },
    fetch: {
      before: function(req, res, context) {
        console.log(context);
        return context.continue;
      }
    }
  }
}

Tesing

GET /api/v0/users/me

Output

{ instance: undefined,
  criteria: {},
  attributes: {},
  skip: [Function],
  stop: [Function],
  continue: [Function],
  error: [Function] }
{ instance: undefined,
  criteria: {},
  attributes: {},
  skip: [Function],
  stop: [Function],
  continue: [Function],
  error: [Function] }
{ instance: undefined,
  criteria: {},
  attributes: {},
  skip: [Function],
  stop: [Function],
  continue: [Function],
  error: [Function] }

Expected

criteria: {
  "id": "me"
}

UPD: Also tried to access to req object directly:

auth: authMiddleware.E.ensureAuthenticated,
fetch: {
  before: function(req, res, context) {
    console.log(context);
    // :me
    if (req.params.id === 'me') {
      context.criteria.id = req.user.id;
    }
    console.log(context);
    return context.continue;
  }
}

Response is:

{
  "message": "\"me\" is not a valid integer",
  "errors": []
}

The request doen't respect criteria: {} object, does it?

@mbroadst
Copy link
Collaborator

mbroadst commented Oct 4, 2015

this looks like its an actual bug here. The logic needs to be something like: if (attribute in req.params && !!criteria[attribute])

@mbroadst
Copy link
Collaborator

mbroadst commented Oct 6, 2015

fixed in 470abb2

@mbroadst mbroadst closed this as completed Oct 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants