TypeError: Cannot create a Parse.File with that data

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

Will it work for NFT? https://ipfs.moralis.io:2053/ipfs/QmNuxgpo3vGppRoZKMBsYU16gq23ydiPCTUcjbj4J23KuC

No, that wonā€™t work, can you show me the line where you make the moralis file instance?

File instance codes:
const fileType = req.file.mimetype;
const value = fileType.substring(fileType.lastIndexOf(ā€™/ā€™) + 1);
const imageFile = new Moralis.File(myNFT.${value}, {base64 : btoa(JSON.stringify(file))});

await imageFile.saveIPFS({useMasterKey: true});
imageURI = imageFile.ipfs();

Multer codes:

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ā€™);

Also have tried without the multer local storage. Not getting the solve yet. Maybe there is the problem with this line only: ā€œconst imageFile = new Moralis.File(myNft.${value}, {base64 : btoa(JSON.stringify(file))});ā€

This as is right now returned the ipfs link you sent above?

Yes. Here is the another link: https://ipfs.moralis.io:2053/ipfs/QmaSGHimt6rpWDUtDu1wZg6qRGcZmjt17kYVnHUjmYhhoD

Have you tried this - I wrote //CHANGE HERE where I made changes

File instance codes:
const fileType = req.file.mimetype;
const value = fileType.substring(fileType.lastIndexOf(ā€™/ā€™) + 1);
const imageFile = new Moralis.File(myNFT.${value}, {base64 : req.file.buffer.toString("base64")}); //CHANGE HERE

await imageFile.saveIPFS({useMasterKey: true});
imageURI = imageFile.ipfs();

Multer codes:

let storage = multer.memoryStorage() //CHANGE HERE

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ā€™);
2 Likes

Yes. I just tried this and this is working perfectly.

Thank you so much man. Thanks a lot. really glad to you.

1 Like

uploadimage = async(fileobj) => {

// const files = new Moralis.File(fileobj);

// await files.saveIPFS({useMasterKey:true});

// console.log(files.ipfs());

// return files.ipfs();



const fileType = fileobj.mimetype;

const value = fileType.substring(fileType.lastIndexOf(`/`) + 1);

const imageFile = new Moralis.File(myNFT.${value}, {base64 : req.file.buffer.toString("base64")});

await imageFile.saveIPFS({useMasterKey: true});

imageURI = imageFile.ipfs();

console.log(imageURI);

}

ERROR

const imageFile = new Moralis.File(myNFT.${value}, {base64 : req.file.buffer.toString(ā€œbase64ā€)});
^

SyntaxError: missing ) after argument list

what to do here please help me out.

my image name is 1.jfif, so is their any need to change anything in myNFT.${value}

what is your myNFT here ? Is it image name ?

You could have it as `myNFT.${value}` rather