如何从React应用访问PCF中的VCAP_SERVICES?

问题描述

我在PCF上托管了一个react应用,并绑定了凭据服务。

推荐的访问凭据的方法是使用process.env.VCAP_SERVICES。

如果我在应用程序内部使用它,则该值将返回为“未定义”。

我应该在.env文件还是manifest.yml文件中使用它?如果可以,怎么办?

另一个推荐的解决方案是使用cfenv程序包,但使用它时出现以下错误:

index.js:117 Uncaught TypeError: Arguments to path.join must be strings
at index.js:117
at Array.filter (<anonymous>)
at filter (index.js:287)
at Object.push../node_modules/path-browserify/index.js.exports.join (index.js:115)
at getPortsFile (index.js:62)
at Object.<anonymous> (index.js:13)
at Object../node_modules/ports/index.js (index.js:65)
at __webpack_require__ (bootstrap:784)
at fn (bootstrap:150)
at Object.<anonymous> (cfenv.js:13)
at Object../node_modules/cfenv/lib/cfenv.js (cfenv.js:314)
at __webpack_require__ (bootstrap:784)
at fn (bootstrap:150)

解决方法

一种方法是使用npm vcap_services

安装:

npm install vcap_services --save

用法:

var vcapServices = require('vcap_services');
var credentials = vcapServices.findCredentials({ service: 'personality_insights' });
console.log(credentials);

更多详细信息here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...