Sails.JS:TypeError:无法读取未定义的属性'pkg'

时间:2017-11-18 22:34:55

标签: javascript node.js sails.js

当我尝试运行我的简单Sails.JS应用程序(包含swaggerui)时,我收到以下错误:

$ sails lift

info: Starting app...

debug: hookPath: DummyPath\Software\MyService\node_modules\sails-swagger\dist\api\hooks\swagger
debug: marlinspike (swagger): loading config from DummyPath\Software\MyService\node_modules\sails-swagger\dist\config
debug: marlinspike (swagger): loading Services from DummyPath\Software\MyService\node_modules\sails-swagger\dist\api\services...
warn: marlinspike (swagger): no Services found. skipping
debug: marlinspike (swagger): loading Models...
debug: marlinspike (swagger): loading Controllers...
debug: marlinspike (swagger): loading Policies...
warn: marlinspike (swagger): no Policies found. skipping
DummyPath\Software\MyService\node_modules\sails-swagger\dist\api\hooks\swagger\index.js:49
        hook.doc = _libXfmr2['default'].getSwagger(_this.sails, _this.sails.config.swagger.pkg);
                                                                                          ^

TypeError: Cannot read property 'pkg' of undefined
    at DummyPath\Software\MyService\node_modules\sails-swagger\dist\api\hooks\swagger\index.js:49:91
    at DummyPath\Software\MyService\node_modules\sails\lib\app\private\after.js:91:14
    at DummyPath\Software\MyService\node_modules\sails\node_modules\async\lib\async.js:721:13
    at DummyPath\Software\MyService\node_modules\sails\node_modules\async\lib\async.js:52:16
    at done (DummyPath\Software\MyService\node_modules\sails\node_modules\async\lib\async.js:246:17)
    at DummyPath\Software\MyService\node_modules\sails\node_modules\async\lib\async.js:44:16
    at Sails.<anonymous> (DummyPath\Software\MyService\node_modules\sails\node_modules\async\lib\async.js:718:17)
    at Sails.<anonymous> (DummyPath\Software\MyService\node_modules\sails\node_modules\async\lib\async.js:167:37)
    at Object.onceWrapper (events.js:293:19)
    at emitNone (events.js:86:13)
    at Sails.emit (events.js:188:7)
    at Sails.emitter.emit (DummyPath\Software\MyService\node_modules\sails\lib\app\private\after.js:50:11)
    at sailsReady (DummyPath\Software\MyService\node_modules\sails\lib\app\lift.js:53:11)
    at DummyPath\Software\MyService\node_modules\sails\node_modules\async\lib\async.js:721:13
    at DummyPath\Software\MyService\node_modules\sails\node_modules\async\lib\async.js:52:16
    at DummyPath\Software\MyService\node_modules\sails\node_modules\async\lib\async.js:269:32

npm install,npm uninstall sails或npm cache clear不起作用。 我已经尝试过再次重新安装grunt-cli并再次使用saig-swagger ...... 有人在这里有解决方案吗?

github.com上的问题也已打开......

1 个答案:

答案 0 :(得分:1)

使用sails-swagger时,您还必须在sails应用程序的config目录中为swagger创建配置文件。此配置文件设置一个pkg变量,该变量指向sails应用程序的package.json文件。

上面的错误基本上是说无法找到属性pkg,因此招摇不起作用。

要解决此问题,请首先在config目录中创建一个名为swagger.js的文件。

然后将以下内容添加到swagger.js文件

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