hemerajs / hemera

🔬 Writing reliable & fault-tolerant microservices in Node.js https://hemerajs.github.io/hemera/
MIT License
806 stars 70 forks source link

Cause: TypeError: Cannot read property 'type' of undefined #52

Closed imatefx closed 7 years ago

imatefx commented 7 years ago

I cannot get any idea of what this error could be, can someone please point out what is the reason for it Hapi Javascript Route plugin, I am using hapi-hemera plugin

'use strict';

const Joi = require('joi');
const Boom = require('boom');

const signupHandler = (request, reply) => {

  return reply.act({ topic: 'signup', cmd: 'register' },
    function (err, result) {
      return reply(err || result)
    })

};

const _routeConfig = {

  description: 'New xyz account signup.',
  notes: 'New xyz account signup.',
  tags: ['api', 'greeting'],
  validate: {
    payload: {
      company_name: Joi.string().required(),
      email: Joi.string().required(),
      first_name: Joi.string().required(),
      last_name: Joi.string().required(),
      mobile: Joi.string().required(),
      address: Joi.object({
        street: Joi.string().required(),
        city: Joi.string().required(),
        state: Joi.string().required(),
        zip: Joi.string().required(),
        country: Joi.string().required()
      }),
      coordinates: Joi.array().items(Joi.number().required(), Joi.number().required())
        .required()
    },
    headers: Joi.object({
    }).unknown()
  },
  pre: [],
  handler: signupHandler,
  auth: {
    strategy: 'auth0-xyz',
    mode: 'required'
  },
  plugins: {
    'hapi-swagger': {
      responses: {
        '200': {
          'description': 'Successfully completed signup process.'
        },
        '503': {
          'description': 'Error occured during signup process.'
        }
      }
    }
  }
};

module.exports = {
  routeConfig: _routeConfig
};

Worker Process Typescript module

import * as Hemera from 'nats-hemera'
import { Client, connect } from 'nats'

const authUrl = 'nats://localhost:4222'
const nats = connect(authUrl)
const hemera = new Hemera(nats, { logLevel: 'info' })

hemera.ready(() => {

  console.log('Ready to listen .. ')

  hemera.add({ topic: 'signup', cmd: 'register' }, (req: any, cb: any) => {

    console.log(JSON.stringify(req, null, 4))

    cb(null, 'req successful')
  })
})

export { hemera, nats, authUrl }

I am getting the following error

On Hapi Route

[2017-03-06T02:21:52.608Z] INFO (app/4624 on Mac-Pro.local): Connected!
Plugin registration and server initialization
  ✔ 5) should be successful (0 ms)
Signup Route
  ✔ 6) should be registered (6 ms)
[2017-03-06T02:21:52.645Z] INFO (app/4624 on Mac-Pro.local): ACT_OUTBOUND - ID:59507afe9e852550836ad49e070e50ca
[2017-03-06T02:21:52.661Z] INFO (app/4624 on Mac-Pro.local): ACT_INBOUND - ID:undefined (NaNms)
[2017-03-06T02:21:52.664Z] FATAL (app/4624 on Mac-Pro.local): Fatal error
    FatalError
        at self.sendRequest (/api-modules/signup/node_modules/nats-hemera/build/index.js:777:23)
        at Object.callback (/api-modules/signup/node_modules/hapi-hemera/node_modules/nats/lib/nats.js:1133:5)
        at Client.processMsg (/api-modules/signup/node_modules/hapi-hemera/node_modules/nats/lib/nats.js:892:11)
        at Client.processInbound (/api-modules/signup/node_modules/hapi-hemera/node_modules/nats/lib/nats.js:824:14)
        at Socket.<anonymous> (/api-modules/signup/node_modules/hapi-hemera/node_modules/nats/lib/nats.js:421:12)
        at emitOne (events.js:96:13)
        at Socket.emit (events.js:189:7)
        at readableAddChunk (_stream_readable.js:176:18)
        at Socket.Readable.push (_stream_readable.js:134:10)
        at TCP.onread (net.js:551:20)
    Cause: SyntaxError: Unexpected token o in JSON at position 1
        at JSON.parse (<anonymous>)
        at Object.exports.parse (/api-modules/signup/node_modules/errio/index.js:191:34)
        at Hemera.<anonymous> (/api-modules/signup/node_modules/nats-hemera/build/index.js:762:35)
        at done (/api-modules/signup/node_modules/items/lib/index.js:31:25)
        at Hemera.<anonymous> (/api-modules/signup/node_modules/nats-hemera/build/index.js:173:7)
        at each (/api-modules/signup/node_modules/nats-hemera/build/ext.js:51:11)
        at iterate (/api-modules/signup/node_modules/items/lib/index.js:36:13)
        at Object.exports.serial (/api-modules/signup/node_modules/items/lib/index.js:39:9)
        at Ext.invoke (/api-modules/signup/node_modules/nats-hemera/build/ext.js:54:11)
        at self.sendRequest (/api-modules/signup/node_modules/nats-hemera/build/index.js:741:48)
        at Object.callback (/api-modules/signup/node_modules/hapi-hemera/node_modules/nats/lib/nats.js:1133:5)
        at Client.processMsg (/api-modules/signup/node_modules/hapi-hemera/node_modules/nats/lib/nats.js:892:11)
        at Client.processInbound (/api-modules/signup/node_modules/hapi-hemera/node_modules/nats/lib/nats.js:824:14)
        at Socket.<anonymous> (/api-modules/signup/node_modules/hapi-hemera/node_modules/nats/lib/nats.js:421:12)
        at emitOne (events.js:96:13)
        at Socket.emit (events.js:189:7)
        at readableAddChunk (_stream_readable.js:176:18)
        at Socket.Readable.push (_stream_readable.js:134:10)
        at TCP.onread (net.js:551:20)

On Worker process

[2017-03-06T02:21:34.659Z] INFO (hemera-f1fa6cc7ea3438aa/4492 on Mac-Pro.local): Connected!
Ready to listen ..
[2017-03-06T02:21:34.670Z] INFO (hemera-f1fa6cc7ea3438aa/4492 on Mac-Pro.local): ADD - ADDED
[2017-03-06T02:21:52.656Z] ERROR (hemera-f1fa6cc7ea3438aa/4492 on Mac-Pro.local): Bad implementation
    ImplementationError
        at Hemera.onServerPreHandler (/worker-modules/signup/node_modules/nats-hemera/build/index.js:670:34)
        at done (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:113:17)
        at Hemera.onServerPreHandler (/worker-modules/signup/node_modules/nats-hemera/build/extensions.js:129:3)
        at each (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:75:16)
        at iterate (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:118:11)
        at Function.serial (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:121:9)
        at Extension.invoke (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:81:17)
        at Hemera.onServerPreRequestHandler (/worker-modules/signup/node_modules/nats-hemera/build/index.js:711:47)
        at done (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:113:17)
        at Hemera.onServerPreRequest (/worker-modules/signup/node_modules/nats-hemera/build/extensions.js:121:3)
    Cause: TypeError: Cannot read property 'type' of undefined
        at /worker-modules/signup/node_modules/nats-hemera/build/index.js:661:46
        at Function.serial (/worker-modules/signup/node_modules/nats-hemera/build/util.js:69:9)
        at Hemera.onServerPreHandler (/worker-modules/signup/node_modules/nats-hemera/build/index.js:648:26)
        at done (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:113:17)
        at Hemera.onServerPreHandler (/worker-modules/signup/node_modules/nats-hemera/build/extensions.js:129:3)
        at each (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:75:16)
        at iterate (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:118:11)
        at Function.serial (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:121:9)
        at Extension.invoke (/worker-modules/signup/node_modules/nats-hemera/build/extension.js:81:17)
        at Hemera.onServerPreRequestHandler (/worker-modules/signup/node_modules/nats-hemera/build/index.js:711:47)
[2017-03-06T02:21:52.663Z] FATAL (hemera-f1fa6cc7ea3438aa/4492 on Mac-Pro.local): process exited
imatefx commented 7 years ago

hapi-hemera was using old hemera

StarpTech commented 7 years ago

Hi @imatefx what was the issue?

imatefx commented 7 years ago

Hapi-hemera was using 0.1.x hemera package, I updated the dependencies of hapi-hemera package, now its working fine.

StarpTech commented 7 years ago

@imatefx thanks for reporting.