Swagger.js无法读取null的属性'get'

时间:2015-05-22 02:28:16

标签: javascript node.js express swagger

我正在尝试为我的express api创建一个Swagger规范和应用程序。我的app.js文件是:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');
var tenants = require('./routes/tenants');

var models = require('./models.js');
var resources = require('./resources.js')

var app = express();
var swagger = require('swagger-node-express').createNew(app);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

swagger.addModels(models);
swagger.addGet(resources.getTenantById);//This is where I am getting the error

swagger.setApiInfo({
  title: "Swagger Sample App",
  description: "This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.wordnik.com\">http://swagger.wordnik.com</a> or on irc.freenode.net, #swagger.  For this sample, you can use the api key \"special-key\" to test the authorization filters",
  termsOfServiceUrl: "http://helloreverb.com/terms/",
  contact: "apiteam@wordnik.com",
  license: "Apache 2.0",
  licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0.html"
});

swagger.configureSwaggerPaths("", "api-docs", "")
swagger.configure("http://localhost:8002", "1.2.0");

app.use('/', routes);
app.use('/users', users);
app.use('/tenants',tenants);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


app.listen(8002);

我的resources.js文件是:

var sw = require('./node_modules/swagger-node-express/Common/node');
var swe = sw.swagger.errors;
var params = sw.paramTypes;
var url = require('url');

exports.getTenantById = {
    'spec' : {
        description : "Operations on Tenants",
        path : "tenants/{tenantId}",
        method : "GET",
        nickname : "getTenantById",
        summary : "Find Tenant by Id",
        notes : "Returns a tenant by Id",
        type : "Tenant",
        produces : ["application/json"],
        parameters : [params.path("tenantId","ID of the Tenant","string")],
        responseMessages : [swe.invalid('tenantId')]
    },
    'action' : function(req,res){
        if(!req.params.tenantId){
            console.log("Tenants Id not provided");
            return res;
        }

        var tenant = {
            "id" : res.params.tenantId
        }

        res.send(JSON.stringify(tenant));
    }
};

我的package.json文件:

{
  "name": "myapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.10.2",
    "cookie-parser": "~1.3.3",
    "debug": "~2.1.1",
    "express": "~4.11.1",
    "jade": "~1.9.1",
    "morgan": "~1.5.1",
    "serve-favicon": "~2.2.0",
    "swagger-node-express": "~2.0"  
  }
}

指向swagger-node-express的链接:https://github.com/swagger-api/swagger-node-express

运行节点app.js命令后我得到的错误:

Users/deepaksharma/Documents/PROGRAMMING/sample-express-app/myapp/node_modules/swagger-node-express/Common/node/swagger.js:446
    app[currentMethod](fullPath, function (req, res, next) {
       ^
TypeError: Cannot read property 'get' of null
    at Swagger.addMethod (/Users/deepaksharma/Documents/PROGRAMMING/sample-express-app/myapp/node_modules/swagger-node-express/Common/node/swagger.js:446:8)
    at /Users/deepaksharma/Documents/PROGRAMMING/sample-express-app/myapp/node_modules/swagger-node-express/Common/node/swagger.js:495:10
    at Function.forOwn (/Users/deepaksharma/Documents/PROGRAMMING/sample-express-app/myapp/node_modules/swagger-node-express/node_modules/lodash/dist/lodash.js:1301:15)
    at Swagger.addHandlers (/Users/deepaksharma/Documents/PROGRAMMING/sample-express-app/myapp/node_modules/swagger-node-express/Common/node/swagger.js:493:5)
    at Swagger.addGet.Swagger.addGET (/Users/deepaksharma/Documents/PROGRAMMING/sample-express-app/myapp/node_modules/swagger-node-express/Common/node/swagger.js:520:8)
    at Object.<anonymous> (/Users/deepaksharma/Documents/PROGRAMMING/sample-express-app/myapp/app.js:31:9)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

我无法理解为什么会出现空错误?

1 个答案:

答案 0 :(得分:0)

我明白了!这是一个愚蠢的错误。我忘了在app.js的第30行添加models.model。我将models.js导入变量模型,但没有在app.js第30行的addModels()中使用导出的变量models.model。

此问题可以关闭,因为它不会为社区添加任何有用的内容。