holidayextras / jsonapi-server

A config driven NodeJS framework implementing json:api and GraphQL
MIT License
488 stars 115 forks source link

I keep getting a 503 from my resource with custom handler #340

Open jrdn91 opened 6 years ago

jrdn91 commented 6 years ago

I'm trying to make a custom handler that will handle my resource for making a PDF, but I keep getting a 503 in my response

I'm only implementing POST here because that's all I'll be using

"use strict";

var PrintHandler = module.exports = function PrintHandler(config) {
  this._config = config;
};

PrintHandler.prototype.ready = false;

PrintHandler.prototype.initialise = function(resourceConfig) {
  self.ready = true;
};

PrintHandler.prototype.create = function(request, newResource, callback) {
  console.log(newResource);
  return callback(null, {
    message:'hello world'
  });
};

Right now I'm just trying to get it working. Why am I getting this error? If I switch to using a real handler like the mongodb on it works, but just not with a custom handler

jrdn91 commented 6 years ago

I was able to get past this error by doing this ...

let PrintHandler = new printHandler();

jsonApi.define({
  resource: "print",
  handlers: PrintHandler,
...

However now I'm getting a general 500 error, I added a search and find since it was complaining about it and I'm attempting to use the search I am getting a generic 500 error, the detail just says ??

I made sure to specify the search params and added them as a query param in postman

Here is my search function

PrintHandler.prototype.search = function (request, callback) {
  const self = this
  console.log(request.route.query)
  return callback(null, {
    message: 'hello world'
  }, 1)
}
paparomeo commented 6 years ago

Can you show me the full code for your custom handler so I can test it locally?

jrdn91 commented 6 years ago

https://gist.github.com/Jordan4jc/73f721482cf826218a5fc13a6bd23f8e

paparomeo commented 6 years ago

One of the issues you have with your search method is that it needs to return an Array. I'm working on a PR which will make this explicit rather than failing with the generic error. Anyway try to change it to return an array with a single element and you should be able to progress.

jrdn91 commented 6 years ago
PrintHandler.prototype.search = function (request, callback) {
  const self = this
  console.log(request.route.query)
  return callback(null, [{
    message: 'hello world'
  }])
}

I'm getting this output in postman

{
    "jsonapi": {
        "version": "1.0"
    },
    "meta": {},
    "links": {
        "self": "/api/v1/print"
    },
    "data": [],
    "included": []
}
paparomeo commented 6 years ago

Try running the server with DEBUG environment variable set to * (e.g. in bash: DEBUG=* npm start). That should provide you debugging output which will make clearer what the problem is.

jrdn91 commented 6 years ago

I see it's complaining about there not being an id property set.

Is there a way I can specify a route handler through express and break out of these jsonapi requirements for just this one route since it's such a specific usage? I tried grabbing the express server and attaching a route handler after doing the readdirSync but it doesn't seem to be doing anything

var app = jsonApi.getExpressServer();
app.get('/test',function(req,res){
  res.send({
    message: 'hello world'
  })
});

Maybe this isn't possible?