Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
Why does solidity smart contract function always return 0 after call through JS?

This is the function from my smart contract:

uint public numberOfFoods;
function returnNumberOfFoods() external view returns(uint){
    return numberOfFoods;
}

I am trying to fetch numberOfFoods using web3. This is my JS code:

const Web3 = require('web3');
const FoodOrder = require('../../build/FoodOrder.json');

const foodItems = async ()=>{    
    const web3 = new Web3(window.ethereum);
    const networkId = await web3.eth.net.getId();
    const myContract = new web3.eth.Contract(
        FoodOrder.abi,
        FoodOrder.networks[networkId].address
    );

    const numberOfFoods = await myContract.methods.returnNumberOfFoods().call();
    return numberOfFoods;
}

export default foodItems;

After receiving the promise, this is what I'm using to fetch the returned value:

import foodItems from "./helpers/number_of_food_items";

foodItems()
.then(data => {
    console.log(data);    //this always return 0
})
.catch(error => {
    // Handle/report error
});

Note: There are other functions in my smart contracts that change the value of numberOfFoods. I've checked those functions and they're changing the value of numberOfFoods correctly as expected. It seems that the problem is with the particular returnNumberOfFoods() function or my JS driver code.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda