senecajs / seneca-transport

Seneca micro-services message transport over TCP and HTTP.
MIT License
63 stars 45 forks source link

seneca.make$ is not a function in transport-utils.js #135

Closed iamNoah1 closed 2 years ago

iamNoah1 commented 8 years ago

Hey guys,

transport-utils.js seems to have problem while responding with an entity created with the Seneca Data Entity API. It tries to invoke seneca.make$(raw) in line 487 and crashes with the following stracktrace (at least for me).

TypeError: seneca.make$ is not a function at internals.Utils.handle_entity (/Users/noa/Desktop/Workspace/stash/seneca_playground/node_modules/seneca-transport/lib/transport-utils.js:487:19) at internals.Utils.handle_response (/Users/noa/Desktop/Workspace/stash/seneca_playground/node_modules/seneca-transport/lib/transport-utils.js:82:19) at /Users/noa/Desktop/Workspace/stash/seneca_playground/node_modules/seneca-transport/lib/http.js:148:25 ...

Some additional Stuff

Server:

var seneca = require('seneca')()

seneca.use('basic')
.use('entity')

seneca.add('role: db,cmd: persist', (msg, reply) => {
  var movie = seneca.make('movie')
  movie.title = msg.title

  movie.save$(function(err, saved_movie){
    if(err){
      reply(err, null);
    } else {
      reply(null, saved_movie)
    }
  })
})

seneca.ready(function(){
  seneca.listen();
})

Client:

var seneca_client = require('seneca')().client();

seneca_client.act({role: 'db', cmd: 'persist', title: "blade2"}, function (err, result) {
  if (err) return console.error(err)
  console.log(result)
})

Package.json:

{
  "name": "seneca_playground",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "seneca": "^3.2.1",
    "seneca-basic": "^0.5.0",
    "seneca-entity": "^1.3.0",
    "seneca-mem-store": "^0.6.0",
    "seneca-mongo-store": "^1.1.0"
  }
}

It would be nice if you could verify this. Thank you

Cheers Noah

jeromevalentin commented 7 years ago

I faced the same issue, and as workaround I'm cleaning my entities prior to send them remotely. As you can see below:

seneca.add('role: db,cmd: persist', (msg, reply) => {
  var movie = seneca.make('movie')
  movie.title = msg.title

  movie.save$(function(err, saved_movie){
    if(err){
      reply(err, null);
    } else {
      reply(null, seneca.util.clean(saved_movie))
    }
  })
})

"Clean" removes all $ properties/functions from the entity, and the client is no more trying to call make$.

Hope this helps, Regards, Jérôme

iamNoah1 commented 7 years ago

Thanks for the workaround ... worked like a charm 👍

MikeLindenau commented 7 years ago

I assume this is not desired behavior?

jeromevalentin commented 7 years ago

FMHO this kind of behavior is application dependant and the choice to send entity remotely must be left to the developer. By the way I don't have tested it, but I'm not sure what will be the behavior when using an entity remotely:

ghost commented 7 years ago

If I'm understanding correctly, the framework is trying to serialize and deserialize an entity, but the entity/plugin isn't defined in the remote system?

Should the framework maybe indicate more about an entity not being loaded instead of an arbitrary error?

Added a comment to https://github.com/jeromevalentin/seneca-transport/commit/465e27ec9169106c95ab9dc1c096bc1b1d426a20, which seems like a good start to fixing the issue.

rjrodger commented 5 years ago

workaround is load seneca-entity plugin