The error says "app.js:152 Uncaught TypeError: Cannot read properties of undefined (reading 'deployed') at Object.submitPatientInfo (app.js:152:37) at HTMLButtonElement.onclick (patientDashboard.html:47:69)"
The contract was deployed when i signed in. Does it mean I can only deploy once? if yes then how sould I handle this?
App = {
webProvider: null,
contracts: {},
account: '0x0',
init: function () {
return App.initWeb();
},
initWeb:function() {
// if an ethereum provider instance is already provided by metamask
const provider = window.ethereum
if( provider ){
// currently window.web3.currentProvider is deprecated for known security issues.
// Therefore it is recommended to use window.ethereum instance instead
App.webProvider = provider;
}
else{
$("#loader-msg").html('No metamask ethereum provider found')
console.log('No Ethereum provider')
// specify default instance if no web3 instance provided
App.webProvider = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
}
return App.initContract();
},
initContract: function () {
$.getJSON("PatientManagement.json", function (patientManagement) {
App.contracts.PatientManagement = TruffleContract(patientManagement);
App.contracts.PatientManagement.setProvider(App.webProvider);
return App.render();
});
},
render: async function () {
const loader = $("#loader");
const content = $("#content");
loader.show();
content.hide();
if (window.ethereum) {
try {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
App.account = accounts[0];
$("#accountAddress").html("Your Account: " + App.account);
} catch (error) {
if (error.code === 4001) {
// User rejected request
console.warn('User rejected');
}
$("#accountAddress").html("Your Account: Not Connected");
console.error(error);
}
}
App.contracts.PatientManagement.deployed()
.then(function (instance) {
return instance.admin();
})
.then(function (adminAddress) {
if (adminAddress.toLowerCase() === App.account.toLowerCase()) {
// Show admin view
$("#adminView").show();
$("#patientView").hide();
} else {
// Show patient view
$("#adminView").hide();
$("#patientView").show();
}
loader.hide();
content.show();
})
.catch(function (error) {
console.warn(error);
});
},
patientSignin: function(){
console.log(App.contracts.PatientManagement)
App.contracts.PatientManagement.deployed()
.then(function (instance) {
return instance.admin();
})
.then(function(adminAddress){
if (adminAddress.toLowerCase()!==App.account.toLowerCase()){
return window.ethereum.request({ method: 'eth_sendTransaction', params:[{
from: App.account,
to: adminAddress,
gas: '50000',
value:'0',
description: 'Patient Sign In',
}] });
}else{
alert("Admin cannot sign in as a patient")
exit() ///Wrong approach
}
})
.then(function(txHash){
window.location.href="patientDashboard.html";
console.log(txHash)
}).catch(function (error) {
console.error(error);
});
},
adminSignin: function () {
App.contracts.PatientManagement.deployed()
.then(function (instance) {
return instance.admin();
})
.then(function (adminAddress) {
if (adminAddress.toLowerCase() === App.account.toLowerCase()) {
return window.ethereum.request({ method: 'eth_sendTransaction', params:[{
from: App.account,
to: adminAddress,
gas: '50000',
value:'0',
description: 'Admin Sign In',
}] });
} else {
alert('You are not authorized to sign in as admin.');
exit() ///Wrong approard
}
})
.then(function(txHash){
window.location.href="adminDashboard.html";
console.log(txHash)
})
.catch(function (error) {
console.error(error);
});
},
submitPatientInfo: function () {
const name = document.getElementById('name').value;
const age = parseInt(document.getElementById('age').value);
const gender = document.getElementById('gender').value;
const vaccineStatus = parseInt(document.getElementById('vaccineStatus').value);
const district = document.getElementById('district').value;
const symptomsDetails = document.getElementById('symptomsDetails').value;
console.log(`${typeof(name)},${typeof(age)},${typeof(gender)},${typeof(vaccineStatus)},${typeof(district)},${typeof(symptomsDetails)}`)
console.log(App.contracts.PatientManagement)
App.contracts.PatientManagement.deployed()
.then(function (instance) {
return instance.addPatient(name, age, gender, vaccineStatus, district, symptomsDetails);
})
.then(function (transaction) {
console.log('Transaction Hash:', transaction.tx);
alert('Patient information added successfully');
// You can redirect or perform other actions here
})
.catch(function (error) {
console.error(error);
alert('Error adding patient information');
});
},
logout: function () {
// Clear the current user account and redirect to index.html
App.account = '0x0';
window.location.href = 'index.html';
},
// Other functions...
};
$(function () {
$(window).load(function () {
App.init();
});
});