sails-swagger icon indicating copy to clipboard operation
sails-swagger copied to clipboard

Cannot set property 'doc' of undefined

Open rashidshkh opened this issue 9 years ago • 33 comments

When i use sails-swagger it says :- error: The bootstrap function threw an error after its callback was called :: Ty peError: Cannot set property 'doc' of undefined. Do you have any documentation about how to implement.

I dig more into code and found this.sails.hooks.swagger get undefined in node_modules\sails-swagger\dist\api\hooks\swagger\index.js

rashidshkh avatar Sep 04 '15 05:09 rashidshkh

I experienced the same issue. The hook was loaded successfully but was not added to sails.hooks.

jekiii avatar Sep 06 '15 01:09 jekiii

Sorry, updating documentation. This hooks currently works only with sails master

tjwebb avatar Sep 12 '15 08:09 tjwebb

Will this make its way into v0.11? I'm not really sure on the timetable for v0.12 and happy to start on 0.12 if it's a relatively mature beta.

yankeeinlondon avatar Sep 13 '15 21:09 yankeeinlondon

0.12 is in "release candidate" stage at the moment. I'm running it in production in a couple of places.

tjwebb avatar Sep 13 '15 22:09 tjwebb

I'm running into problems getting it running with handlebars templates so for now I've reverted to 0.11.1; how painful is the migration to 0.12.x?

yankeeinlondon avatar Sep 13 '15 22:09 yankeeinlondon

The upgrade path to 0.12 should be pretty painless. Can you file a more specific issue in the Sails repo?

tjwebb avatar Sep 14 '15 01:09 tjwebb

@tjwebb, sure will do. thanks for your feedback here.

yankeeinlondon avatar Sep 14 '15 07:09 yankeeinlondon

Is there migration from v0.11.x to v0.12.x instructions?

I am really excited to use sails-swagger however it is erroring with this issue.

Glavin001 avatar Sep 30 '15 15:09 Glavin001

Following. Just got the same thing on 0.11.2 and 0.12.0-rc3.

drewlio avatar Oct 08 '15 09:10 drewlio

Works on master for me, but not 0.12.0-rc3

AlexisNo avatar Oct 09 '15 20:10 AlexisNo

sails is now on 0.11.2 as master, and i have te same error

devildeveloper avatar Oct 11 '15 07:10 devildeveloper

Got the same error here:

sails lift

info: Starting app...

debug: hookPath: /home/ghazali/node-app/node_modules/sails-swagger/dist/api/hooks/swagger
debug: marlinspike (swagger): loading config from /home/ghazali/node-app/node_modules/sails-swagger/dist/config
debug: marlinspike (swagger): loading Services from /home/ghazali/node-app/node_modules/sails-swagger/dist/api/services...
warn: marlinspike (swagger): no Services found. skipping
debug: marlinspike (swagger): loading Controllers...
debug: marlinspike (swagger): loading Policies...
warn: marlinspike (swagger): no Policies found. skipping
info: 
info:                .-..-.
info: 
info:    Sails              <|    .-..-.
info:    v0.11.2             |\
info:                       /|.\
info:                      / || \
info:                    ,'  |'  \
info:                 .-'.-==|/_--'
info:                 `--'-------' 
info:    __---___--___---___--___---___--___
info:  ____---___--___---___--___---___--___-__
info: 
info: Server lifted in `/home/ghazali/version-control/actigage/actigage-api`
info: To see your app, visit http://localhost:1337
info: To shut down Sails, press <CTRL> + C at any time.

debug: --------------------------------------------------------
debug: :: Sun Oct 11 2015 22:39:55 GMT+0700 (WIB)

debug: Environment : development
debug: Port        : 1337
debug: --------------------------------------------------------
error: The bootstrap function threw an error after its callback was called :: TypeError: Cannot set property 'doc' of undefined
 at /home/ghazali/node-app/node_modules/sails-swagger/dist/api/hooks/swagger/index.js:49:18
 at /home/ghazali/node-app/node_modules/sails/lib/app/private/after.js:91:14
 at /home/ghazali/node-app/node_modules/sails/node_modules/async/lib/async.js:251:17
 at done (/home/ghazali/node-app/node_modules/sails/node_modules/async/lib/async.js:132:19)
 at /home/ghazali/node-app/node_modules/sails/node_modules/async/lib/async.js:32:16
 at /home/ghazali/node-app/node_modules/sails/node_modules/async/lib/async.js:248:21
 at Sails.<anonymous> (/home/ghazali/node-app/node_modules/sails/node_modules/async/lib/async.js:572:34)
 at Sails.g (events.js:260:16)
 at emitNone (events.js:72:20)
 at Sails.emit (events.js:166:7)
 at Sails.emitter.emit (/home/ghazali/node-app/node_modules/sails/lib/app/private/after.js:50:11)
 at sailsReady (/home/ghazali/node-app/node_modules/sails/lib/app/lift.js:47:11)
 at /home/ghazali/node-app/node_modules/sails/node_modules/async/lib/async.js:251:17
 at /home/ghazali/node-app/node_modules/sails/node_modules/async/lib/async.js:154:25
 at /home/ghazali/node-app/node_modules/sails/node_modules/async/lib/async.js:248:21
 at /home/ghazali/node-app/node_modules/sails/node_modules/async/lib/async.js:612:34 [TypeError: Cannot set property 'doc' of undefined]

muhammadghazali avatar Oct 11 '15 15:10 muhammadghazali

This hook currently works only with sails 0.12

Masquer avatar Oct 11 '15 16:10 Masquer

Any update on this ? I get the same error using 0.11.2 and 0.12.

kevinblanco avatar Oct 13 '15 02:10 kevinblanco

The problem seems to be about the name of the hook. The hook itself assume it is sails.hooks.swagger, but with 0.12.0-rc3, it seems that the name is not cleaned and the hook is available as sails.hooks['sails-swagger']. Using the master branch of Sails, the hook is correctly loaded.

You can change the way sails loads an installable hook to load the hook correctly with 0.12.0-rc3. I didn't test it with 0.11.x.

// config/installedHooks.js
module.exports.installedHooks = {
   "sails-swagger": {
      // load the hook into sails.hooks.swagger instead of sails.hooks['sails-swagger']
      "name": "swagger"
   }
};

Then you can load the swagger document at http://127.0.0.1:1337/swagger/doc/

Edit You may have to create this file too (Inspired by the permissions-api generator) to access to the hook's actions

// api/controllers/SwaggerController.js

var _ = require('lodash');
var _super = require('sails-swagger/dist/api/controllers/SwaggerController');

_.merge(exports, _super);
_.merge(exports, {

    // Extend with custom logic here by adding additional fields, methods, etc.

});

AlexisNo avatar Oct 20 '15 18:10 AlexisNo

I had to create both files in my project directory. In order to get it to run, I had to install lodash (npm install lodash --save)

svenarndt avatar Nov 04 '15 12:11 svenarndt

@AlexisNo I have added both files you described (and also installed lodash). Swagger spec is available form http://localhost:1337/swagger/doc but how do you use swagger-ui then ?

lucj avatar Nov 24 '15 22:11 lucj

Swagger UI is not part of sails-swagger.

From the Swagger UI README file:

Swagger UI is a dependency-free collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation and sandbox from a Swagger-compliant API. Because Swagger UI has no dependencies, you can host it in any server environment, or on your local machine. swagger-ui is independent.

You can quickly have a Swagger UI app running on your local environment using docker

docker build -t swagger-ui-builder .
docker run -p 127.0.0.1:8080:8080 swagger-ui-builder

This will start Swagger UI at http://localhost:8080. By default, CORS should be correctly configured for this address. But I think the current NPM version does not include it. You'll have to configure it yourself in your project.

// config/routes.js
module.exports.routes = {
  '/swagger/doc': {
    cors: {
      origin: 'http://localhost:8080',
      methods: 'GET,OPTIONS,HEAD'
    },
    controller: 'SwaggerController',
    action: 'doc'
  }
}

Also, use config/swagger.js to configure the address where http://localhost:1337/swagger/ui will redirect.

// config/swagger.js
module.exports.swagger = {
  /**
   * require() the package.json file for your Sails app.
   */
  pkg: require('../package'),
  ui: {
    url: 'http://localhost:8080'
  }
};

AlexisNo avatar Nov 25 '15 14:11 AlexisNo

Thanks a lot for the details answer ! Got it working :)

lucj avatar Nov 25 '15 20:11 lucj

I got a problem with the CORS, even after doing what you said : the Access-Control-Allow-Origin header coming from my sailsjs app is empty, i can't find anything online.

adrien-candiotti avatar Dec 04 '15 10:12 adrien-candiotti

@adrien-candiotti I also have this problem so I copied the swagger-ui elements in an assets/docs folder instead, so ui is on the same machine/port. It seems I did not manage to setup the CORS correctly.

lucj avatar Dec 04 '15 10:12 lucj

@adrien-candiotti @lucj the problem with CORS is the config is hard coded https://github.com/tjwebb/sails-swagger/issues/13. I believe the issue has been fixed in further commits in master, but a new released version has not been added to npm to publish the fix

moisesrodriguez avatar Dec 11 '15 17:12 moisesrodriguez

@moisesrodriguez Hi, thanks a lot for the update.

lucj avatar Dec 11 '15 22:12 lucj

So i read all the thread and was wondering if there was a fix for Sails v0.11.3. ?

Thanks.

manodupont avatar Dec 21 '15 21:12 manodupont

I merged https://github.com/tjwebb/sails-swagger/pull/18, I just need to publish to npm. You can use master for now

tjwebb avatar Dec 21 '15 22:12 tjwebb

Thanks a lot Travis !

On Mon, Dec 21, 2015 at 11:40 PM, Travis Webb [email protected] wrote:

I merged #18 https://github.com/tjwebb/sails-swagger/pull/18, I just need to publish to npm. You can use master for now

— Reply to this email directly or view it on GitHub https://github.com/tjwebb/sails-swagger/issues/3#issuecomment-166443905.

Luc Juggery - https://about.me/lucjuggery

lucj avatar Dec 22 '15 08:12 lucj

@AlexisNo I did the steps as you have said but I am not getting the swagger Ui but only the json format document . I didn't understand the docker part please help me @lucj

ghost avatar Dec 30 '15 11:12 ghost

@tjwebb would you mind publishing to npm. I'm getting this error on sails 0.12.0-rc4 and when I try to use master by doing this on my package.json "sails-swagger": "git://github.com/tjwebb/sails-swagger.git", it says it cannot find the module even thought it there.

moisesrodriguez avatar Jan 13 '16 16:01 moisesrodriguez

Thanks @AlexisNo and @lucj - I was able to get it all running with your instructions! Now I need to figure out how to have the swagger-ui allow me to edit the post/put directly in swagger-ui like the demo.

@tjwebb I look forward to the npm push as well. Let me know if I can help.

Since I am not running off master for either project. I combined comments from this thread and hope it helps others:

  1. npm install --save lodash ( thx @svenarndt )
  2. Created the controller
// api/controllers/SwaggerController.js

var _ = require('lodash');
var _super = require('sails-swagger/dist/api/controllers/SwaggerController');

_.merge(exports, _super);
_.merge(exports, {
  // Extend with custom logic here by adding additional fields, methods, etc.
});
  1. Created the installHooks.js
// config/installedHooks.js
module.exports.installedHooks = {
  "sails-swagger": {
    // load the hook into sails.hooks.swagger instead of sails.hooks['sails-swagger']
    "name": "swagger"
  }
};
  1. Create swagger.js hook:
// config/swagger.js
module.exports.swagger = {
  /**
   * require() the package.json file for your Sails app.
   */
  pkg: require('../package'),
  ui: {
      url: 'http://swagger.balderdash.io'
  }
};
  1. Downloaded swagger-ui and copyied the dist/ contents to assets/docs under my current webapp as @lucj did. I am sure the docker way works well but I didn't try it as I wanted it all under the same app.
  2. Done. (At least I hope I got everything).

ralberts avatar Jan 17 '16 15:01 ralberts

@ralberts thanks for your summary, the only thing I did not get is how to navigate to /assets/docs to actually open the swagger.io on local server.

talamaska avatar Jan 20 '16 14:01 talamaska