sails-swagger
sails-swagger copied to clipboard
Cannot set property 'doc' of undefined
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
I experienced the same issue. The hook was loaded successfully but was not added to sails.hooks.
Sorry, updating documentation. This hooks currently works only with sails master
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.
0.12 is in "release candidate" stage at the moment. I'm running it in production in a couple of places.
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
?
The upgrade path to 0.12 should be pretty painless. Can you file a more specific issue in the Sails repo?
@tjwebb, sure will do. thanks for your feedback here.
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.
Following. Just got the same thing on 0.11.2 and 0.12.0-rc3.
Works on master for me, but not 0.12.0-rc3
sails is now on 0.11.2 as master, and i have te same error
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]
This hook currently works only with sails 0.12
Any update on this ? I get the same error using 0.11.2 and 0.12.
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.
});
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)
@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 ?
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'
}
};
Thanks a lot for the details answer ! Got it working :)
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 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.
@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 Hi, thanks a lot for the update.
So i read all the thread and was wondering if there was a fix for Sails v0.11.3. ?
Thanks.
I merged https://github.com/tjwebb/sails-swagger/pull/18, I just need to publish to npm. You can use master for now
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
@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
@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.
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:
- npm install --save lodash ( thx @svenarndt )
- 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.
});
- 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"
}
};
- 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'
}
};
- 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.
- Done. (At least I hope I got everything).
@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.