0

I am beginner in writing Dapp. I am writing with truffle and solidity.

Rental.sol

    pragma solidity ^0.5.0;

    contract Rental {
    // address[16] public rentals;
    // address public owner;

   struct Car {
    uint id;
    string make;
    string model;
    uint year;
    bool available;
    uint price;

  }

  mapping(uint => Car) public cars;

  uint public carsCount;

  constructor () public {
    addCars("Chevrolet", "Suburban", 1955, true, 2500);
    addCars("Ford", "Suburban", 1955, true, 2500);
    addCars("BMW", "Suburban", 1955, true, 2500);
    addCars("Chevrolet", "Suburban", 1955, true, 2500);
    addCars("Chevrolet", "Suburban", 1955, true, 2500);
    addCars("Chevrolet", "Suburban", 1955, true, 2500);
}

function addCars(string memory _make, string memory _model, uint _year, bool _available, uint _price) private {
    carsCount ++;
    cars[carsCount] = Car(carsCount, _make, _model, _year, _available, _price);
}

app.js render function

render: function()
{

    var rentalInstance;

    App.contracts.Rental.deployed().then(function(instance){
        rentalInstance = instance;
        return rentalInstance.carsCount();

    }).then(function(carsCount){
        var carsDiv = $("#cars");
        carsDiv.empty();

        for (var i = 1; i <= carsCount; i++)
        {
            alert(carsCount);
            rentalInstance.cars(i).then(function(car){
                var make = car[1];
                var model = car[2];
                var year = car[3];                
                var available = car[4];
                var price = car[5];       

                var carTemplate = "<h3>"+ make +"</h3><h4>"+ model + year + 
                "</h4><button className= 'pure-button pure-button-primary' disabled =" + available +
                 "Rent Me for Ξ" + price + "</button>" 

                 carsDiv.append(carTemplate);                    
            }); 
        }
    });

}

}

I got following error in console

app.js:50 Uncaught (in promise) TypeError: rentalInstance.carsCount is not a function at app.js:50

  • which truffle and web3js version are you using? try return rentalInstance.carsCount.call() – Majd TL Jun 4 at 16:25

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.