0

A dummy here trying to learn dapp.

I referred to a similar issue post here: Cannot read property 'deployed' of undefined issue.

But I couldn't understand why the error occurred in mine.

App.js

App = {
 web3Provider: null,
 contracts: {},

 initWeb3: async function() {
    // Modern dapp browsers...
  if (window.ethereum) {
    App.web3Provider = window.ethereum;
    try {
      // Request account access
      await window.ethereum.enable();
    } catch (error) {
      // User denied account access...
      console.error("User denied account access")
    }
  }
  // Legacy dapp browsers...
  else if (window.web3) {
    App.web3Provider = window.web3.currentProvider;
  }
  // If no injected web3 instance is detected, fall back to Ganache
  else {
    App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
  }
  web3 = new Web3(App.web3Provider);

      return App.initContract();
},

initContract: function() {
$.getJSON('StudentDetails.json', function(data) {
  // Get the necessary contract artifact file and instantiate it with @truffle/contract
  App.contracts.StudentDetail = TruffleContract(data);

  // Set the provider for our contract
  App.contracts.StudentDetail.setProvider(App.web3Provider);

  // Use our contract to retrieve and mark the adopted pets
  return App.init();
});

return App.AddStudentDetailButton();
},

AddStudentDetailButton: function() {
$(document).on('click','.addMarkDetail',App.AddMark);
},

AddMark: function(event) {
 var post = document.getElementById('mark').value
var postInstance;

App.contracts.StudentDetail.deployed().then(function(instance){
  postInstance = instance;
  return postInstance.studentGrades(post);
});
console.log("marks added");

},

init: async function() {
// load grades
var postInstance;
App.contracts.StudentDetail.deployed().then(function(instance) {
  postInstance = instance;
  return postInstance.getGrades();
}).then(function(result) {
  var counts = result.c[0];

  console.log("Total student marks: "+counts);

  for (var i = 1; i <= counts; i++) {
    postInstance.grades(i).then(function(result) {

      console.log("Enroller address: "+result[0]);
      console.log("grades:"+result[1]);

      var studentRow = $('#studentsRow');
      var postTemplete = $('#infoTemplete');

      postTemplete.find('.panel-title').text(result[0]);
      postTemplete.find('.desc').text(result[1]);
      studentRow.append(postTemplete.html());
    });
    }
  });
 }

};

 $(function() {
   $(window).load(function() {
      App.init();
   });
 });

StudentDetail.sol

pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;

 /*
     Student enrollment in courses and displaying grades 
    of the available student in the enrolled courses.
    
 */

contract StudentDetails{

address public enroller;
mapping(address => Student) students;
mapping(address => Courses) courses;
mapping(address => Grades) grades;
string public g;

address[] public studentAccts;
uint public studentCount;

Student[] enrolledStudents;

constructor() public {
    enroller = msg.sender;
}

modifier onlyEnroller {
    require(msg.sender==enroller);
    _;
}

struct Student {
    address studentID;
    uint rollnumber;
    string name;
    bool registered;
    string enrollments;
}

struct Courses {
    address courseid;
    string coursename;
    uint creditnumber;
}

struct Grades {
    uint rollnumber;
    address courseid;
    uint marks;
    string grade;
}

/* Student registration */  
function registerStudent(address _address, uint _rollnumber, string memory _name, string memory _enrollments) public onlyEnroller {
    require(students[_address].registered==false,"Already registered...");
    students[_address]=Student(_address,_rollnumber,_name, true, _enrollments);
}


function getstudent(address _address) view public returns(address, uint, string memory, bool, string memory) {
    return (students[_address].studentID, students[_address].rollnumber, students[_address].name, students[_address].registered, students[_address].enrollments);
}

/* Create course */ 
function createCourses(address _courseId, string memory _courseName, uint _creditNumber) public onlyEnroller {
    courses[_courseId]=Courses(_courseId,_courseName,_creditNumber);
}

function getCourse(address _courseId) view public returns(address, string memory, uint) {
    return (courses[_courseId].courseid, courses[_courseId].coursename, courses[_courseId].creditnumber);
}

/* Student grades*/ 
function studentGrades(address _studentID, uint _rollnumber, address _courseId, uint _marks) public{
    
    if(_marks >= 80 && _marks <= 100) {
        g = "A";
    }
    else if(_marks >= 70 && _marks < 80) {
        g = "B";
    }
    else if (_marks >= 60 && _marks < 70) {
        g = "C";
    }
    else {
        g = "F";
    }
    grades[_studentID] = Grades(_rollnumber,_courseId,_marks,g);
}

function getGrades(address _studentID) view public returns(string memory) {
    return (grades[_studentID].grade);
}

 }

error on console

app.js:64 Uncaught (in promise) TypeError: Cannot read property 'deployed' of undefined
at Object.init (app.js:64)
at app.js:93
at dispatch (jquery.min.js:3)
at r.handle (jquery.min.js:3)

Please help me comprehend why the error points to the App.contracts.StudentDetail.deployed().then(function(instance) {. ? StudentDetail is name of the smart contract. Is it correct approach?

  • What is 'Assignment1' ? Moreover there is no such 'StudentDetail' contract in your code. Please clarify. – clement Sep 28 at 13:04
  • oh mistake, its StudentDetail instead of Assignment1. I will update the question with smart contract. – Wangmo.J Sep 28 at 13:14
  • Ok, can you share the declaration of the 'StudentDetail' contract in the js code ? – clement Sep 28 at 13:18
  • According to the error, your contract is not defined. – clement Sep 28 at 13:20
  • oh okay. declaration in app.js? – Wangmo.J Sep 28 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.