[SOLVED] TypeError: Cannot read properties of undefined (reading 'File')

Hi,
Im trying to upload an image to IPFS to make a NFT minter with Next.js and Node but im getting this error following the moralis documentation when i try new Moralis.File(file.name, file).

const onSubmit = async (e) =>  {
        e.preventDefault();
        console.log("name: "+ name)
        console.log('description: ' + description)
        try {
            console.log("===================")
            //save image to IPFS
            console.log(data)
            const file1 = new Moralis.File(data.name, data);
            console.log(file1)
            await file1.saveIPFS();
            console.log('=====', file1) 
            const file1url = file1.ipfs;

            //generate metadata and save to ipfs
            const metadata = {
                name, description, image: file1url
            }
            const file2 = new Moralis.File(`${name}metadata.json`, {
                base64: Buffer.from(JSON.stringify(metadata)).toString('base64')
            });
            await file2.saveIPFS();
            const metadataurl = file2.ipfs();
            //interact with smart contracts
            const contract = new web3.eth.Contract(contractABI, contractAddress);
            const response = await contract.methods.mint(metadataurl).send({ from: account.get('ethAddress')});
            //const tokenId = response.events.Transfer.returnValues.tokenId;
            alert(
                'NFT successfully minted. Contract address - ${contractAddress} and Token Id - ${tokenId}'
            );
        } catch(err) {
           console.error(err);
           alert('Something went wrong'); 
        }
    }

If you want to upload a file to ipfs with Moralis v2 sdk then you have to use uploadFolder api function that can be used to upload a file or multiple files in the same ipfs folder.

If the error is from this line than maybe Moralis is not defined

I have Moralid defined with:
const { Moralis } = require("moralis").default;
I will try with uploadFolder instead of Moralis.File

I have tried with uploadFolder and now Im getting this error: TypeError: Cannot read properties of undefined (reading β€˜EvmApi’).

I have done npm install @moralisweb3/common-evm-utils and defined the const EvmChain so i dont know where is the problem

    let fs = require("fs");
    let axios = require("axios");
    const { EvmChain } = require('@moralisweb3/common-evm-utils');

const onSubmit = async (e) =>  {
        e.preventDefault();
        console.log("name: "+ name)
        console.log('description: ' + description)
        
        try {
            const abi = [
                {
                    path: "http://localhost:3000/files/Captura.png",
                    content: data.toString("base64"),
                },
                ];
            const response = await Moralis.EvmApi.ipfs.uploadFolder({ abi });
                console.log(response.toJSON());
            alert(
                'NFT successfully minted. Contract address - ${contractAddress} and Token Id - ${tokenId}'
            );
        } catch(err) {
           console.error(err);
           alert('Something went wrong'); 
        }
    }

this is in backend or in front end?

what version of moralis sdk did you install?

it doesn’t look like you imported Moralis in that code

Finally i have uploaded a file to ipfs successfully with uploadFolder. I had to do little changes on my code, Thank you so much

2 Likes

I am happy to hear we could help you; may I know how are you finding moralis services so far?

1 Like