Yes, this is the code
Frontend
//here I initialize the json and pass to a function
const metadata = {
"PlotID": plotID.toString(),
"PlotX": land.tile.x.toString(),
"PlotY": land.tile.y.toString(),
"LocationX": land.absPosition.x.toString(),
"LocationY": land.absPosition.y.toString(),
"imageHash": land.imageHash.toString(),
"imageIpfs": land.imageIpfs.toString()
}
const metadataFile = await savePlotDataToIPFS(metadata);
//function
async function savePlotDataToIPFS(metadata) {
const params = {
ppath: "metadata.json",
pcontent: window.btoa(JSON.stringify(metadata))
}
const res = await handleApiPost('moralis/save-plot', params);
if (res.res == "OK")
return res.msg;
else
return null;
}
//function
const handleApiPost = async (endpoint, params) => {
const result = await axios.post(`${serverUrl}/${endpoint}`, params, {
headers: {
'Content-Type': 'application/json',
},
});
return result.data;
};
Here is my Backend
exports.savePlotToIPFS = async (req, res) => {
//check the req body
if (!req.body) {
res.status(400).send({
res: "KO",
msg: "No data"
});
return;
}
const abi = [
{
path: req.body.ppath.toString(),
content: req.body.pcontent.toString()
},
];
try {
const response = await Moralis.EvmApi.ipfs.uploadFolder({
abi
});
res.status(200).send({
res: "OK",
msg: {
"ipfs": response.jsonResponse[0].path,
"hash": response.jsonResponse[0].path.split('/')[4]
}
});
} catch(err){
res.status(400).send({
res: "KO",
msg: err
});
}
}
Thatās all