Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set property 'doc' of undefined #3

Open
rashidshkh opened this issue Sep 4, 2015 · 33 comments
Open

Cannot set property 'doc' of undefined #3

rashidshkh opened this issue Sep 4, 2015 · 33 comments

Comments

@rashidshkh
Copy link

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

@jekiii
Copy link

jekiii commented Sep 6, 2015

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

@tjwebb
Copy link
Member

tjwebb commented Sep 12, 2015

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

@yankeeinlondon
Copy link

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.

@tjwebb
Copy link
Member

tjwebb commented Sep 13, 2015

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

@yankeeinlondon
Copy link

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?

@tjwebb
Copy link
Member

tjwebb commented Sep 14, 2015

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

@yankeeinlondon
Copy link

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

@Glavin001
Copy link

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.

@drewlio
Copy link

drewlio commented Oct 8, 2015

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

@AlexisNo
Copy link
Contributor

AlexisNo commented Oct 9, 2015

Works on master for me, but not 0.12.0-rc3

@devildeveloper
Copy link

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

@muhammadghazali
Copy link

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]

@Masquer
Copy link

Masquer commented Oct 11, 2015

This hook currently works only with sails 0.12

@kevinblanco
Copy link

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

@AlexisNo
Copy link
Contributor

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.

});

@svenarndt
Copy link

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)

@lucj
Copy link

lucj commented Nov 24, 2015

@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 ?

@AlexisNo
Copy link
Contributor

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'
  }
};

@lucj
Copy link

lucj commented Nov 25, 2015

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

@adrien-candiotti
Copy link

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.

@lucj
Copy link

lucj commented Dec 4, 2015

@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.

@moisesrodriguez
Copy link
Contributor

@adrien-candiotti @lucj the problem with CORS is the config is hard coded #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

@lucj
Copy link

lucj commented Dec 11, 2015

@moisesrodriguez Hi, thanks a lot for the update.

@manodupont
Copy link

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

Thanks.

@tjwebb
Copy link
Member

tjwebb commented Dec 21, 2015

I merged #18, I just need to publish to npm. You can use master for now

@lucj
Copy link

lucj commented Dec 22, 2015

Thanks a lot Travis !

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

I merged #18 #18, I just
need to publish to npm. You can use master for now


Reply to this email directly or view it on GitHub
#3 (comment).

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

@ghost
Copy link

ghost commented Dec 30, 2015

@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

@moisesrodriguez
Copy link
Contributor

@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.

@ralberts
Copy link

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).

@talamaska
Copy link

@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.

@blanco217
Copy link

@ralberts Thanks for your explination it helped me get the ui up and running.

@talamaska you will go to http://localhost:1337/docs

@ronsuez
Copy link

ronsuez commented Feb 1, 2016

does anyone knows how to enable POST/PUT requests from the local swaggerUI?

@pankajdaffodil
Copy link

Please let me know how can I send data in query string?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests