TypeError: Cannot create a Parse.File with that data

I am using NodeJS and Multer to upload a file and I am getting this error. Please help me to solve it out. Here is my code:

const fileType = req.file.mimetype;
const value = fileType.substring(fileType.lastIndexOf(’/’) + 1);

const nftImage = new Moralis.File(nft.${value}, req.file);
console.log(nftImage);

Please help me to solve it out and provide a a documentation too to upload NFT images to IPFS and save data to moralis.

What is the error and here are the docs for uploading to ipfs

The error is [Cannot create a Parse.File with that data]. I am uploading image using nodejs and multer.

And I am following the docs but it saves image NFT to moralis. I want to check it to IPFS too and want to create a folder storage in IPFS. How can I do that?

I am sorry if my questions looks odd to you.

The file is saved onto ipfs when you call file.saveIPFS(). The docs example just stores it in the db as well, you could only store the url or the hash in the db (you do need to save something though, so you know where the file is saved)

You can upload folders to ipfs as well, but you cannot add more files/delete them from the folder once you have uploaded the folder. Here is a link to the docs

Okay great. Thank you so much. I got it and please help me to solve out the problem. I am getting this error from this line:

const imageFile = new Moralis.File(myNft.${value}, req.file.originalname );

Also I have tried with this line of code:

const imageFile = new Moralis.File(myNft.${value}, {base64 : btoa(JSON.stringify(req.file.originalname))});

and it returns “https://ipfs.moralis.io:2053/ipfs/QmfAXajYikFht7DsTL9YMdbawNmg2D9e4sKMPqKYJ2TyMy” this instead of showing the image. Is it okay? or from where I can get the image?

The 2nd way is correct, but you are converting a string to base64 instead of an actual file. You have to use base64 on an actual file data, not a string

Can you please let me know how to do it? Please.

Could you console.log(req.file) and show a screenshot?

Yes sure. It returns file

And if I write this code:
const imageFile = new Moralis.File(myNfts.${value}, {base64 : btoa(JSON.stringify(file))});
It returns this “https://ipfs.moralis.io:2053/ipfs/QmeyZJWbzTCN2vE3NnZowUMJ2j2PGbhtCNLWtmLQYYopun

So I tried doing it, and looks like it might have to be in a “normal” javascript format, because it worked for me

Just like in the docs

    let myFile = document.getElementById("fileInput").files[0]
    console.log(myFile);
    let file = new Moralis.File(myFile.name, myFile);
    await file.saveIPFS();
    console.log(file.ipfs());
    console.log(file.hash());

How are you getting the file into the request?

image

I am working with the nodejs and in backend document.getElementById(“fileInput”) is not work for me. I am fetching the file using multer and nodejs.

Try doing
const imageFile = new Moralis.File(`myNft.${value}`, req.file.buffer.toString("base64");

Sorry but it is not working yet and showing that “TypeError: Cannot read properties of undefined (reading ‘toString’)” error. Maybe it not getting any data to convert into base64.

Please help me out of this problem. Couldn’t solve this problem.

Try adding this after you import multer

const storage = multer.memoryStorage()
const upload = multer({ storage: storage })

I did all of these. Here is the req.file file

But it is not working with:
const imageFile = new Moralis.File(myNft.${value}, req.file.buffer.toString(“base64”);

and showing this error "TypeError: Cannot read properties of undefined (reading ‘toString’)”.

Are you calling multer({dest: "something")} anywhere?

Here is my multer code:

let storage = multer.memoryStorage({
destination: (req, file, cb) => cb(null, ‘uploads/’),
})

let upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
if (file.mimetype == “image/png” || file.mimetype == “image/jpg” || file.mimetype == “image/jpeg”) {
cb(null, true);
} else {
cb(null, false);
return cb(new ApiError(4002, ‘Only .png, .jpg and .jpeg format allowed!’));
}
},
limits: {
fileSize: 1024 * 1024 * 1
},
}).single(‘image’);

Try not passing that object into multer.memoryStorage on the first line