Serverless: Serverless.yml中的引用变量错误地呈现为“ [Object object]”

创建于 2016-12-21  ·  3评论  ·  资料来源: serverless/serverless

这是(错误报告/功能建议)

描述

serverless.yml文件中使用site_config: ${file(./config/${opt:stage}/site.yml)}语法时,稍后在无服务器文件中引用的变量(例如: site_secret_key: ${self:site_config.site_secret_key} )在process.env.site_secret_key作为[Object object]应该是与./config/${opt:stage}/site.yml文件中的site_secret_key值关联的字符串值。

请注意,使用以下语法可以正常工作:
${file(./config/${opt:stage}/site.yml):site_secret_key}

附加数据

  • 您正在使用的无服务器框架版本:1.3.0
  • 作业系统:OSX 10.12.1
  • 堆栈跟踪
  • 提供程序错误消息

最有用的评论

这是我的配置演示问题:

service: test-service

plugins:
  - debug-plugin

config: ${file(config.json)}
configEnv: ${file(config.json):env}
configEnvProp: ${file(config.json):env.development}

provider:
  name: aws
  stage: development
  runtime: nodejs4.3
  environment:
    NODE_ENV: ${self:provider.stage}
    fileConfig: ${file(config.json)}
    fileConfigEnv: ${file(config.json):env}
    fileConfigEnvProp: ${file(config.json):env.development}
    selfConfig: ${self:config}
    selfConfigEnv: ${self:configEnv}
    selfConfigEnvProp: ${self:configEnvProp}
    selfConfigDotEnv: ${self:config.env}
    selfConfigDotEnvDotProp: ${self:config.env.development}

functions:
  hello:
    handler: handler.hello

我编写了一个插件来转储serverless.service而没有serverless父项:

% sls debug config
{ service: 'test-service',
  provider:
   { name: 'aws',
     stage: 'development',
     runtime: 'nodejs4.3',
     environment:
      { NODE_ENV: 'development',
        fileConfig:
         { env:
            { development: { region: 'ap-northeast-2' },
              production: { region: 'us-west-1' } },
           assets: { bucket: 'some-bucket' } },
        fileConfigEnv:
         { development: { region: 'ap-northeast-2' },
           production: { region: 'us-west-1' } },
        fileConfigEnvProp: { region: 'ap-northeast-2' },
        selfConfig: undefined,
        selfConfigEnv: undefined,
        selfConfigEnvProp: undefined,
        selfConfigDotEnv: {},
        selfConfigDotEnvDotProp: undefined },
     region: 'us-east-1',
     variableSyntax: '\\${([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}' },
  defaults:
   { stage: 'development',
     region: 'us-east-1',
     variableSyntax: '\\${([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}' },
  custom: undefined,
  plugins: [ 'debug-plugin' ],
  functions:
   { hello:
      { handler: 'handler.hello',
        events: [],
        name: 'test-service-development-hello' } },
  resources: undefined,
  package: {} }

不太确定会导致该输出的原因!

插件来源供参考:

class Debug {
  constructor(serverless, options) {
    this.serverless = serverless;
    this.options = options;

    this.commands = {
      debug: {
        usage: 'Debugging',
        commands: {
          config: {
            usage: 'Show configuration value',
            lifecycleEvents: [
              'print'
            ]
          }
        }
      }
    };

    this.hooks = {
      'debug:config:print': () => this.printConfig(),
    };
  }

  printConfig() {
    const config = Object.assign({}, this.serverless.service);
    delete config.serverless;
    const output = require('util').inspect(config, { colors: true, depth: null });
    console.log(output.substring(0, 100000)); // Paranoia: can't kill node sync output on Ubuntu on Windows?
  }
}

module.exports = Debug;

所有3条评论

这是我的配置演示问题:

service: test-service

plugins:
  - debug-plugin

config: ${file(config.json)}
configEnv: ${file(config.json):env}
configEnvProp: ${file(config.json):env.development}

provider:
  name: aws
  stage: development
  runtime: nodejs4.3
  environment:
    NODE_ENV: ${self:provider.stage}
    fileConfig: ${file(config.json)}
    fileConfigEnv: ${file(config.json):env}
    fileConfigEnvProp: ${file(config.json):env.development}
    selfConfig: ${self:config}
    selfConfigEnv: ${self:configEnv}
    selfConfigEnvProp: ${self:configEnvProp}
    selfConfigDotEnv: ${self:config.env}
    selfConfigDotEnvDotProp: ${self:config.env.development}

functions:
  hello:
    handler: handler.hello

我编写了一个插件来转储serverless.service而没有serverless父项:

% sls debug config
{ service: 'test-service',
  provider:
   { name: 'aws',
     stage: 'development',
     runtime: 'nodejs4.3',
     environment:
      { NODE_ENV: 'development',
        fileConfig:
         { env:
            { development: { region: 'ap-northeast-2' },
              production: { region: 'us-west-1' } },
           assets: { bucket: 'some-bucket' } },
        fileConfigEnv:
         { development: { region: 'ap-northeast-2' },
           production: { region: 'us-west-1' } },
        fileConfigEnvProp: { region: 'ap-northeast-2' },
        selfConfig: undefined,
        selfConfigEnv: undefined,
        selfConfigEnvProp: undefined,
        selfConfigDotEnv: {},
        selfConfigDotEnvDotProp: undefined },
     region: 'us-east-1',
     variableSyntax: '\\${([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}' },
  defaults:
   { stage: 'development',
     region: 'us-east-1',
     variableSyntax: '\\${([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}' },
  custom: undefined,
  plugins: [ 'debug-plugin' ],
  functions:
   { hello:
      { handler: 'handler.hello',
        events: [],
        name: 'test-service-development-hello' } },
  resources: undefined,
  package: {} }

不太确定会导致该输出的原因!

插件来源供参考:

class Debug {
  constructor(serverless, options) {
    this.serverless = serverless;
    this.options = options;

    this.commands = {
      debug: {
        usage: 'Debugging',
        commands: {
          config: {
            usage: 'Show configuration value',
            lifecycleEvents: [
              'print'
            ]
          }
        }
      }
    };

    this.hooks = {
      'debug:config:print': () => this.printConfig(),
    };
  }

  printConfig() {
    const config = Object.assign({}, this.serverless.service);
    delete config.serverless;
    const output = require('util').inspect(config, { colors: true, depth: null });
    console.log(output.substring(0, 100000)); // Paranoia: can't kill node sync output on Ubuntu on Windows?
  }
}

module.exports = Debug;

@simonbuchan我猜问题是您对变量引用的定义变得不确定? 我认为原因是您在功能和资源旁边拥有configconfigEnvconfigEnvProb属性定义为serverless.yml第一父级...等。 如果我没记错的话,我们在serverless.yml对自定义属性设置了限制,使其只能在custom属性中加载。 因此,您可以尝试将自定义属性移动到custom对象中,如下所示:

custom:
  config:
  configEnv:
  ...

并且不要忘记更新您的参考

由于变量需要嵌套在custom@eahefnawy上面的建议suggested,因此关闭

此页面是否有帮助?
0 / 5 - 0 等级