esbenp / pdf-bot

🤖 A Node queue API for generating PDFs using headless Chrome. Comes with a CLI, S3 storage and webhooks for notifying subscribers about generated PDFs
MIT License
2.63k stars 142 forks source link

lowdb -> queue.addToQueue(...).then is not a function #21

Closed nathanpride closed 6 years ago

nathanpride commented 6 years ago

Hi,

I've tried to get this working on a Dockerised node image and natively on a Windows 10 machine. The error is consistent.

TypeError: queue.addToQueue(...).then is not a function
    at /usr/local/lib/node_modules/pdf-bot/src/api.js:30:10
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/pdf-bot/node_modules/express/lib/router/layer.js:95:5)
    at next (/usr/local/lib/node_modules/pdf-bot/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/usr/local/lib/node_modules/pdf-bot/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/pdf-bot/node_modules/express/lib/router/layer.js:95:5)
    at /usr/local/lib/node_modules/pdf-bot/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/usr/local/lib/node_modules/pdf-bot/node_modules/express/lib/router/index.js:335:12)
    at next (/usr/local/lib/node_modules/pdf-bot/node_modules/express/lib/router/index.js:275:10)
    at jsonParser (/usr/local/lib/node_modules/pdf-bot/node_modules/body-parser/lib/types/json.js:119:7)
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/pdf-bot/node_modules/express/lib/router/layer.js:95:5)

The config is:

var htmlPdf = require('html-pdf-chrome')
var lowDB = require('/usr/local/lib/node_modules/pdf-bot/src/db/lowdb')

module.exports = {
    api: {
        port: process.env.PDF_BOT_PORT,
        token: process.env.PDF_BOT_TOKEN
    },
    generator: {
        // Triggers that specify when the PDF should be generated
        completionTrigger: new htmlPdf.CompletionTrigger.Timer(process.env.PDF_BOT_COMPLETION_TIMER),
        // The port to listen for Chrome (default: 9222)
        port: process.env.CHROME_DEBUG_PORT
    },
    storagePath: 'storage',
    db:lowDB({
        lowdDbOptions: {},
//        path:'./storage'
    })
}

Docker image - node version: v10.5.0 Windows node: v8.9.4 pdf-bot: 0.5.4

With and without db options in the config but to with no luck, what am I doing wrong?

Thanks

nathanpride commented 6 years ago

Ok, this was down to luser error.

I was using Postman to test the service, and although JSON (application/json) was displayed once selecting raw, it was not setting the header correctly, therefore the data.url was undefined. Toggling the encoding type made it work.

However this took debugging the application, within addToQueue of queue.js I think it should be:

  if (!data.url || !utils.isValidUrl(data.url)) {
    throw error.createErrorResponse(error.ERROR_INVALID_URL)
  }

  if (data.meta && typeof data.meta !== 'object') {
    throw error.createErrorResponse(error.ERROR_META_IS_NOT_OBJECT)
  }

instead of:

  if (!data.url || !utils.isValidUrl(data.url)) {
    return error.createErrorResponse(error.ERROR_INVALID_URL)
  }

  if (data.meta && typeof data.meta !== 'object') {
    return error.createErrorResponse(error.ERROR_META_IS_NOT_OBJECT)
  }
H3mul commented 4 years ago

This is still an issue; this causes an 500 error on the API if you dont include a body in your POST request, among other things.