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
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ā);
Yes. I just tried this and this is working perfectly.
Thank you so much man. Thanks a lot. really glad to you.
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