ServerLess使用sequelize-typescript模型给出空异常吗?

时间:2019-01-03 08:40:59

标签: aws-lambda serverless-framework sequelize-typescript

我正在尝试让Sequelize与我的aws-lambda项目一起工作。但是,每当我尝试使用模型并且不了解原因时,无服务器框架都会返回一个空异常。 som无服务器的响应只是一对红色括号('{}')...

我将其简化为示例:

import { APIGatewayEvent, Callback, Context, Handler } from 'aws-lambda';
import { Column, Model, PrimaryKey, Sequelize , Table } from 'sequelize-typescript';

@Table
class SomeModel extends Model<SomeModel> {
  @Column
  @PrimaryKey
  public id: number;
}

export const handler: Handler = (event: APIGatewayEvent, context: Context, cb: Callback) => {
  const sequelize = new Sequelize({
    host: '...',
    database: '...',
    dialect: 'mssql',
    username: '...',
    password: '...',
  });

  sequelize.addModels([SomeModel]);

  sequelize.authenticate()
    .then(() => {
      console.log('Connection has been established successfully.');

      const dbversion = sequelize.options.databaseVersion;
      const response = {
        statusCode: 200,
        body: JSON.stringify('DB version: ' + dbversion),
      };
      cb(null, response);
    })
    .catch(err => {
      console.error('Unable to connect to the database:', err);
    });
};

如果我运行“无服务器调用本地--verbose --function dbtest”,则会得到:

{}

  Error --------------------------------------------------

  Exception encountered when loading C:\Users\Thomas\Documents\GitHub\Test\.webpack\service\functions\dbtest\get-dbtest

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: Exception encountered when loading C:\Users\Thomas\Documents\GitHub\Test\.webpack\service\functions\dbtest\get-dbtest
    at AwsInvokeLocal.invokeLocalNodeJs (C:\Users\Thomas\AppData\Roaming\npm\node_modules\serverless\lib\plugins\aws\invokeLocal\index.js:307:13)
    at AwsInvokeLocal.invokeLocal (C:\Users\Thomas\AppData\Roaming\npm\node_modules\serverless\lib\plugins\aws\invokeLocal\index.js:131:19)
From previous event:
    at Object.invoke:local:invoke [as hook] (C:\Users\Thomas\AppData\Roaming\npm\node_modules\serverless\lib\plugins\aws\invokeLocal\index.js:27:10)
    at BbPromise.reduce (C:\Users\Thomas\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:391:55)
From previous event:
    at PluginManager.invoke (C:\Users\Thomas\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:391:22)
    at PluginManager.run (C:\Users\Thomas\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:422:17)
    at variables.populateService.then.then (C:\Users\Thomas\AppData\Roaming\npm\node_modules\serverless\lib\Serverless.js:157:33)
    at runCallback (timers.js:794:20)
    at tryOnImmediate (timers.js:752:5)
    at processImmediate [as _immediateCallback] (timers.js:729:5)
From previous event:
    at Serverless.run (C:\Users\Thomas\AppData\Roaming\npm\node_modules\serverless\lib\Serverless.js:144:8)
    at serverless.init.then (C:\Users\Thomas\AppData\Roaming\npm\node_modules\serverless\bin\serverless:44:28)
    at <anonymous>

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information -----------------------------
     OS:                     win32
     Node Version:           8.10.0
     Serverless Version:     1.35.1

但是,如果我删除/注释了包括addModels()和所有相关导入的模型类,那么它将按预期工作!在这种情况下,我会在响应中获取正确版本的数据库...

最近几天我一直在挠头,我的结论是我必须错过一些基本的,很可能是简单的要点...

想法?

更新-找到解决方法了吗?

我认为我发现了失败的地方...我仍然不明白为什么...

如果我改变了模型上@Column和@PrimaryKey装饰器的顺序,那么一切都会按预期进行...所以主键必须是第一个,否则它将抛出空的异常...但是我想知道为什么吗?

更新2 看来sequelize-typescript应该可以告诉我这种错误...为什么在运行invoke-command时却没有显示出来? https://github.com/RobinBuschmann/sequelize-typescript/blob/93330a700b11e1eaf7fdc53b05fe0b3d92eeb22b/lib/services/models.ts#L115-L118

0 个答案:

没有答案