-2

I have used this:

var seneca = require('seneca')();

seneca.act('role:web',{use:{
   prefix: '/cam',
   pin: {role:'api',type:'*'},
   map:{
   asset: {GET:true,POST:true}      
   }
 }});
seneca.add({role: "api",type: "asset"}, function(args, done) {
done(null, {response: "An example asset"});
});


var express = require('express');
var bodyparser = require('body-parser');
var web = require('seneca-web');
var app = express();
app.use(bodyparser.json());
app.use( web );
app.listen(3000);

The above example gives the following error when I try to call the api using POSTMAN url : http://localhost:3000/cam/asset with post param json as {"role":"api","type":"asset"}

TypeError: Cannot read property 'util' of undefined at web (C:\node_projects\node_modules\seneca-web\web.js:25:22) at Layer.handle [as handle_request] (C:\node_projects\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\node_projects\node_modules\express\lib\router\index.js:312:13) at C:\node_projects\node_modules\express\lib\router\index.js:280:7 at Function.process_params (C:\node_projects\node_modules\express\lib\router\index.js:330:12) at next (C:\node_projects\node_modules\express\lib\router\index.js:271:10) at expressInit (C:\node_projects\node_modules\express\lib\middleware\init.js:33:5) at Layer.handle [as handle_request] (C:\node_projects\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\node_projects\node_modules\express\lib\router\index.js:312:13) at C:\node_projects\node_modules\express\lib\router\index.js:280:7

And it also prints the following JSON on the console. Something related to NO MATCHING ACTION PATTERN FOUND.

{"err":{},"level":"warn","when":1481878030070} {"notice":"seneca: No matching action pattern found for { use: { prefix: '/cam', pin: { role: 'api', type: '' }, map: { asset: [Object], invoice: [Object] } }, role: 'web' }, and no default result provided (using a default$ property).","code":"act_not_found","err":{"eraro":true,"orig":null,"code":"act_not_found","seneca":true,"package":"seneca","msg":"seneca: No matching action pattern found for { use: { prefix: '/cam', pin: { role: 'api', type: '' }, map: { asset: [Object], invoice: [Object] } }, role: 'web' }, and no default result provided (using a default$ property).","details":{"args":"{ use: { prefix: '/cam', pin: { role: 'api', type: '' }, map: { asset: [Object], invoice: [Object] } }, role: 'web' }","plugin":{}},"callpoint":"at handle_inward_break (C:\node_projects\node_modules\seneca\seneca.js:1155:23)"},"actid":"gh3fzeqpdsp0/so5yjiwk3v1l","msg":{"use":{"prefix":"/cam","pin":{"role":"api","type":""},"map":{"asset":{"GET":true,"POST":true},"invoice":{"GET":true,"POST":true}}},"role":"web","meta$":{"id":"gh3fzeqpdsp0/so5yjiwk3v1l","tx":"so5yjiwk3v1l"}},"meta":{},"listen":false,"transport":{},"kind":"act","case":"ERR","duration":260,"level":"error","when":1481878030071}

Shobhit Srivastava
  • 501
  • 1
  • 6
  • 16
  • `TypeError: Cannot read property 'util' of undefined` - there's no `util` anywhere in that code - so how can that be the error? Perhaps the rest of the error message will show where this error occurs – Jaromanda X Dec 16 '16 at 07:06
  • The error is received on POSTMAN when try to call the api like : http://localhost:3000/cam/asset with post param json as {"role":"api","type":"asset"} @JaromandaX – Shobhit Srivastava Dec 16 '16 at 08:10
  • Could you make those errors a little more readable – Jaromanda X Dec 16 '16 at 08:57
  • Looking at seneca-web `web.js` - where the error occurs - `module.exports = function web (options) { var seneca = this var extend = seneca.util.deepextend` - it seems that `this` when using seneca-web is expected to be a `seneca` object - the way you are requiring seneca-web, it won't be - none of the examples in seneca-web documentation look like your code - so, you need to read the documentation and figure out where you are going wrong, or hope that someone will for you – Jaromanda X Dec 16 '16 at 09:02

1 Answers1

3

Below is a working example:

'use strict'

 var Seneca = require('seneca')
 var Express = require('express')
 var Web = require('seneca-web')

 var Routes = [{
     prefix: '/api',
     pin: 'role:api,cmd:*',
     map: {
          home: {GET: true}
          }
     }]
 var seneca = Seneca()

 var config = {
     routes: Routes,
     adapter: require('seneca-web-adapter-express'),
     context: Express()
 }

 seneca.client()
.use(Web, config)
.ready(() => {
    var server = seneca.export('web/context')()
    server.listen('4000', () => {
        console.log('server started on: 4000')
    })
})

 seneca.add({role: 'api',cmd:'home'}, function(args, done) {
     done(null, {response:"hey"});
     });
Shobhit Srivastava
  • 501
  • 1
  • 6
  • 16