still the same error Cannot create a Parse.File with that data.
can u explain the code? Ive been testing it for hours and so far this is what i have for the cloud function
Moralis.Cloud.define("upload_function", async (request) => {
const object = {"key" : "value"}
const nftFileMetadataFile = new Moralis.File("metadata.json", {base64 : btoa(JSON.stringify(object))});
x = await nftFileMetadataFile.save({useMasterKey:true});
console.log(JSON.stringify(x))
return x;
});
idk if im calling it correctly
const serverUrl = "https://5afgk9nae19l.usemoralis.com:2053/server";
const appId = "lhSPMJb08k6pB25ESY87s7VxvnNZobzHLWHbs8yj";
Moralis.start({ serverUrl, appId });
async function login() {
hideElement(createItemForm);
let user = Moralis.User.current();
if (!user) {
user = await Moralis.authenticate({ signingMessage: "Log in using Moralis" })
.then(function (user) {
console.log("logged in user:", user);
console.log(user.get("ethAddress"));
})
.catch(function (error) {
console(error);
});
initUser();
}
}
initUser = async () => {
if (await Moralis.User.current())
{
hideElement(userConnectButton);
showElement(userProfileButton);
showElement(openCreateItemButton);
}
else
{
showElement(userConnectButton);
hideElement(userProfileButton);
hideElement(openCreateItemButton);
}
}
initUser();
logOut = async () =>{
await Moralis.User.logOut();
hideElement(userInfo);
initUser();
}
openUserInfo = async () => {
user = await Moralis.User.current();
if (user)
{
const email = user.get('email');
if(email){
userEmailField.value = email;
}else{
userUsernameField.value = "";
}
userUsernameField.value = user.get('username');
const userAvatar = user.get('avatar');
if(userAvatar){
userAvatarImg.src = userAvatar.url();
showElement(userAvatarImg)
} else{
hideElement(userAvatarImg);
}
showElement(userInfo);
}
else
{
login();
}
}
saveUserInfo = async () => {
user.set('email', userEmailField.value);
user.set('username', userUsernameField.value);
if (userAvatarFile.files.length > 0){
// Moralis can handle any kind of file but her we specify jpegS
const avatar = new Moralis.File("avatar.jpg", userAvatarFile.files[0]);
//dont worry about name colisions theyr given names and track by element Id
user.set('avatar', avatar);
}
await user.save();
alert("We got your info stored!");
openUserInfo();
}
createItem = async () => {
if (createItemFile.files.length == 0){
alert("Are you gonna select a file?");
return;
} else if (createItemNameField.value.length == 0){
alert("You need to give the file a name!");
return;
}
const nftFile = new Moralis.File("nftFile.mp4",createItemFile.files[0]);
await nftFile.saveIPFS();
const nftFilePath = nftFile.ipfs();
const nftFileHash = nftFile.hash();
const metadata = {
name: createItemNameField.value,
description: createItemDescriptionField.value,
nftFilePath: nftFilePath,
nftFileHash: nftFileHash
};
// const nftFileMetadataFile = new Moralis.File("metadata.json", {base64: Buffer.from('metadata').toString('base64')});
async function upload_function(){
const cloudFile = await Moralis.Cloud.run("upload_function");
const nftFileMetadataFile = new Moralis.File("metadata.json", { base64: cloudFile });
console.log(nftFileMetadataFile);
}
upload_function();
await nftFileMetadataFile.saveIPFS();
const nftFileMetadataFilePath = nftFileMetadataFile.ipfs();
const nftFileMetadataFileHash = nftFileMetadataFile.hash();
const Item = Moralis.Object.extend("Item");
//create new instance of the class
const item = new Item();
item.set('name', createItemNameField.value);
item.set('description', createItemDescriptionField.value);
item.set('nftFilePath', nftFilePath.value);
item.set('nftFileHash', nftFileHash.value);
item.set('nftFileMetadataFilePath', nftFileMetadataFilePath.value);
item.set('MetadataFileHash', nftFileMetadataFileHash.value);
await item.save();
// console.log(item);
}
hideElement = (element) => element.style.display = "none";
showElement = (element) => element.style.display = "block";
//NavBar
const userConnectButton = document.getElementById("btnConnect");
userConnectButton.onclick = login;
const openCreateItemButton = document.getElementById("btnOpenCreateItem");
openCreateItemButton.onclick = () => showElement(createItemForm);
//User Profile
const userProfileButton = document.getElementById("btnUserInfo");
userProfileButton.onclick = openUserInfo;
const userInfo = document.getElementById("userInfo");
const userUsernameField = document.getElementById("txtUsername");
const userEmailField = document.getElementById("txtEmail");
const userAvatarImg = document.getElementById("imgAvater");
const userAvatarFile = document.getElementById("fileAvatar");
document.getElementById("btnCloseUserInfo").onclick = () => hideElement(userInfo);
document.getElementById("btnSaveUserInfo").onclick = saveUserInfo;
document.getElementById("btnLogout").onclick = logOut;
//Item Creation
const createItemForm = document.getElementById("createItem");
const createItemNameField = document.getElementById("txtCreateItemName");
const createItemDescriptionField = document.getElementById("txtCreateItemDescription");
const createItemPriceField = document.getElementById("numberCreateItemPrice");
const createItemStatusField = document.getElementById("selectCreateItemStatus");
const createItemFile = document.getElementById("fileCreateItemFile");
document.getElementById("btnCloseCreateItem").onclick = () => hideElement(createItemForm);
document.getElementById("btnCreateItem").onclick = createItem;