1

I created a function that registers births and use a form to submit the data. The problem is every time I try to use web3/Javascript, it gives me this error. The error is because of the dateOfBirth parameter as you've seen on value = "2022-05-18". In the html form, the input type is "date". Can someone help with a way I can get around this?

Uncaught Error: invalid BigNumber string (argument="value", value="2022-05-18", code=INVALID_ARGUMENT, version=bignumber/5.6.0)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Registration {
    struct Child {
        uint256 id;
        string name;
        string sex;
        uint256 weight;
        string condition;
        string otherConditions;
        uint256 dateOfBirth;
        string placeOfBirth;
        string birthOccurence;
        string motherFullname;
        string motherNrc;
        string contact;
        string pAddress;

    }

    //create struct type array to store values
    Child[] public child;
    uint256 nextId = 1;

    function find(uint256 _id) public view returns (uint256) {
        for (uint256 i; i < child.length; i++) {
            if (child[i].id == _id){ return i;
            }
        }
        revert("Record does not exist.");
    }

    function insertRecord(
        string memory _name,
        string memory _sex,
        uint256 _weight,
        string memory _condition,
        string memory _otherConditions,
        uint256 _dateOfBirth,
        string memory _placeOfBirth,
        string memory _birthOccurence,
        string memory _motherFullname,
        string memory _motherNrc,
        string memory _contact,
        string memory _pAddress
    ) public {
        child.push(Child(nextId, _name, _sex, _weight, _condition, _otherConditions, _dateOfBirth,
         _placeOfBirth, _birthOccurence, _motherFullname, _motherNrc, _contact, _pAddress));
        nextId++;
    }

    function readRecordById(uint256 _id)
        public
        view
        returns (uint256, string memory, string memory, uint256, 
        string memory, string memory, uint256, string memory, string memory,
        string memory, string memory, string memory, string memory)
    {
        uint256 i = find(_id);
        return (
            child[i].id,
            child[i].name,
            child[i].sex,
            child[i].weight,
            child[i].condition,
            child[i].otherConditions,
            child[i].dateOfBirth,
            child[i].placeOfBirth,
            child[i].birthOccurence,
            child[i].motherFullname,
            child[i].motherNrc,
            child[i].contact,
            child[i].pAddress
        );
    }


    function deleteRecordById(uint256 _id) public returns (uint256) {
        uint256 i = find(_id);
        delete child[i];
    }
}

2 Answers 2

0

You need to pay the contract for that execution because it changes the state. Make the method payable, pass { value: '25000000000' } or whatever the cost will be in wei. Convert the date to uint256/64 or 32.

0

The problem is that the struct Child is expecting a uint256 for dateOfBirth. The argument you're trying to pass into it is the string "2022-05-18". On your client side, you could parse the date into a UNIX timestamp like so:

new Date('2022-05-18').getTime()
>>> 1652832000000

When you read from the blockchain on your client side again, you'll have to parse it back into the string

const date = new Date(1652832000000)
let dateFormatted = date.toISOString();
dateFormatted = dateFormatted.slice(0, dateFormatted.indexOf("T"))
>>> '2022-05-18'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.