fabcar/javascript/server.js
//opt = doctor OR opt = patient
opt = doctor OR opt = patient
opt = doctor OR opt = patient
async function main() {
        try {
            const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json');
            const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
            const walletPath = path.join(process.cwd(), 'wallet');
            const wallet = await Wallets.newFileSystemWallet(walletPath);
            console.log(`Wallet path: ${walletPath}`);
            const identity = await wallet.get(email);
            if (!identity) {
                console.log('An identity for the user '+email+' does not exist in the wallet');
                console.log('Run the registerUser.js application before retrying');
                return;
            }
            const gateway = new Gateway();
            await gateway.connect(ccp, { wallet, identity: email, discovery: { enabled: true, asLocalhost: true } });
            const network = await gateway.getNetwork('mychannel');
            const contract = network.getContract('fabcar');
            const result = await contract.evaluateTransaction('queryAccType',opt);
            console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
            res.status(200).send({response: JSON.parse(`${result}`)})
        } catch (error) {
            console.error(`Failed to evaluate transaction: ${error}`);
            res.status(500).json({error: `${error}`})
            process.exit(1);
        }
    }
async function main ( ) { try { const ccpPath = path . resolve ( __dirname , '..' , '..' , 'first-network' , 'connection-org1.json' ) ; const ccp = JSON . parse ( fs . readFileSync ( ccpPath , 'utf8' ) ) ; const walletPath = path . join ( process . cwd ( ) , 'wallet' ) ; const wallet = await Wallets . newFileSystemWallet ( walletPath ) ; console . log ( ` ${ walletPath } ` ) ; const identity = await wallet . get ( email ) ; if ( ! identity ) { console . log ( 'An identity for the user ' + email + ' does not exist in the wallet' ) ; console . log ( 'Run the registerUser.js application before retrying' ) ; return ; } const gateway = new Gateway ( ) ; await gateway . connect ( ccp , { wallet , identity : email , discovery : { enabled : true , asLocalhost : true } } ) ; const network = await gateway . getNetwork ( 'mychannel' ) ; const contract = network . getContract ( 'fabcar' ) ; const result = await contract . evaluateTransaction ( 'queryAccType' , opt ) ; console . log ( ` ${ result . toString ( ) } ` ) ; res . status ( 200 ) . send ( { response : JSON . parse ( ` ${ result } ` ) } ) } catch ( error ) { console . error ( ` ${ error } ` ) ; res . status ( 500 ) . json ( { error : ` ${ error } ` } ) process . exit ( 1 ) ; } }
opt = doctor OR opt = patient
opt = doctor OR opt = patient
// load the network configuration // Create a new file system based wallet for managing identities. // Check to see if we've already enrolled the user. // Create a new gateway for connecting to our peer node. // Get the network (channel) our contract is deployed to. // Get the contract from the network. // Evaluate the specified transaction. // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4') // queryAllCars transaction - requires no arguments, ex: ('queryAllCars')