我的Firebase云功能有问题。当调用我的函数时,与infura的连接失败。我已经升级到提到here的Blaze计划,但是它没有改变。 这是我的代码:
const Web3 = require('web3')
exports.getInstructorOnBlock = functions.database.ref('/services/{serviceId}').onUpdate((snapshot, context) => {
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/MyKey"));
}
var abi = [{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"instructorAccts","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"fName","type":"string"},{"indexed":false,"name":"lName","type":"string"},{"indexed":false,"name":"age","type":"uint256"}],"name":"instructorInfo","type":"event"},{"constant":false,"inputs":[{"name":"_address","type":"address"},{"name":"_age","type":"uint256"},{"name":"_fName","type":"string"},{"name":"_lName","type":"string"}],"name":"setInstructor","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getInstructors","outputs":[{"name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getInstructor","outputs":[{"name":"","type":"uint256"},{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"countInstructors","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];
var address = "address of the contract";
var MyContract = web3.eth.contract(abi);
var SmartContract = MyContract.at(address);
web3.eth.defaultAccount = web3.eth.accounts[0];
SmartContract.getInstructor(web3.eth.defaultAccount,"address", (err, res) => {
console.log("+++ res +++ " + res);
if (err) {
console.log("getInstructor fail" + err);
}
});
return 0
});
我也尝试过重新安装npm软件包。这是我的package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"ansicolor": "^1.1.72",
"axios": "^0.18.0",
"dotenv": "^6.0.0",
"eth": "^4.0.1",
"ethereumjs-tx": "^1.3.6",
"ethereumjs-util": "^5.2.0",
"express": "^4.16.3",
"firebase-admin": "^5.12.1",
"firebase-functions": "^1.0.4",
"npm": "^6.1.0",
"ololog": "^1.1.103",
"personal": "^1.0.1",
"web3": "github:ethereum/web3.js"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.8.0"
},
"private": true
}
和firebase返回的错误:
Error: CONNECTION ERROR: Couldn't connect to node https://rinkeby.infura.io/key.
at Object.InvalidConnection (/user_code/node_modules/web3/lib/web3/errors.js:31:16)
at HttpProvider.send (/user_code/node_modules/web3/lib/web3/httpprovider.js:93:18)
at RequestManager.send (/user_code/node_modules/web3/lib/web3/requestmanager.js:58:32)
at Eth.get [as accounts] (/user_code/node_modules/web3/lib/web3/property.js:107:62)
at exports.getInstructorOnBlock.functions.database.ref.onUpdate (/user_code/index.js:112:39)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36
有人知道我的问题吗?
请注意,我可以在html页面上通过相同的infura / rinkeby访问我的智能合约“ getInstructor”功能
谢谢