So the file path and hash keeps coming back undefined when logging for the item metadata. vstudio is telling me that the old syntax is now depricated and I noticed in a later totorial filip uses the tostring method over the btoa. how can I implement this new synatx?
the older method (from the clone rarible in 24 hours tutorial)
and this is the more recent method ive seen(bulk upload tutorial)
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 (createItemFile.value.length == 0){
alert("You need to give he 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 : btoa(JSON.stringify(metadata))});
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;