问题描述
我在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