[SOLVED] Btoa depricated, New syntax for getting base 64

this is only an example of a variable name named object, you can call it metadata, and it has some key value data as you would have in a metadata.

now this error,
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘value’)
at HTMLButtonElement.createItem

this looks like an error in that code, it looks like a variable is undefined and it tries to access .value for that variable

Ok so here is the create item function.


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 he file a name!");
    return;
  }

  const nftFile = new Moralis.File("nftFile.jpg",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 btoa2 = function(str){
    return Buffer.from(str).toString('base64');
    }

//  const object = {"key" : "value"}
 const file = new Moralis.File("metadata.json", {base64 : btoa2(JSON.stringify(metadata))});
 nftFileMetadataFile = await file.save();
 console.log(JSON.stringify(nftFileMetadataFile))
 
//  upload_function(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);
}

and here is the error


you save it here twice?

still same error

probably you have to use file.saveIPFS() instead of file.save()

This works but it also brings me back to the original problem I had days ago. The hash value is coming back undefined. Im starting to think its because of the extend items object
just seen item.save() maybe it should be item.saveIPFS()

you could add more info before the printed variables with console.log, like console.log("nftFileMetadataFile:", JSON.strinfigy(nftFileMetadataFile))

now I don’t know each line where from is in your output

We almost got it!
I hope this isnt a security issue but i had to change item from a const to a var so i could get the values from IPFSbut its still not printing what it should. Whats with the underscore in front of each of the fields?

  const Item = Moralis.Object.extend("Item");
  //create new instance of the class
  var 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);
  item = await file.saveIPFS();
  console.log(item);
}

I don’t know exactly what is with all those _, I think that I saw them before

Solved
rookie mistake on my part. I had a value in each of those field but their wasnt any actual input. I must say I learned alot In my journey but this is why I really need a team. That mistake was a simple syntax error but it cost me days of progress. of course I now have a better understanding of metadata now and I can see how when I get better in the future I can use real world data in my Dapps. I also see that I have a long way to go with cloud functions but at least now I have a road map! Thanks for being responsive through all this, I couldnt have done it without you.

1 Like

Looks like I have I more minor issue:


the blue link takes me where i want to go

but the link highlighted in yellow takes me here


it turns out this did work like u originally suggest I just didnt realize it because of the syntax erorr. so yea 100% solved this time

1 Like

it looks like you uploaded she string ‘metadata’ on that yellow link instead of the information contained in that metadata variable

I also got confused by VS code mentioning ‘btoa deprecated’, but further reading it seems only related to the use in Node, see https://stackoverflow.com/questions/68849233/convert-a-string-to-base64-in-javascript-btoa-and-atob-are-deprecated