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

I see that you have this code directly in your main.js, this is not going to work when the blowser loads the window

where do you call that cloud function in your code?

you also have to add that btoa function in cloud as it is not accessible by default

You can clearly see where I called the function upload_function in the code above. So before I use btoa as a cloud function I need to add a prototype like I would with a header file for a library? Do you have any good study resources on cloud functions? the short video on cloud functions didnt observe this kind of case. I dont really know where to start and I thought my issue was with the way Im parsing the metadata. This is my first time working with cloud functions and I have no Idea what to do. Im very literate and can solve the issue myself but you know that as a dev half the battle is just finding the right resources. The moralis docs dont aknowledge this particular issue and its been 3 days now on this 1 problem so obviously the issue is my own understanding. This back and forth isnt helping me. I spent the entire day yesterday studying JSON and now im going to spend today researching cloud functions, Surely there is an example of someone using a cloud function like this?

one problem is that .saveIPFS is not yet available in cloud functions

you can test this from normal code, after you authenticate:

const btoa2 = function(str){ return Buffer.from(str).toString('base64'); }

  const object = {"key" : "value"}
  const file = new Moralis.File("file_2.json", {base64 : btoa2(JSON.stringify(object))});
  x = await file.save();
  console.log(JSON.stringify(x))

I think the problrem is 2 assumptions are being made her. You are assuming that I understand how a cloud function works and Im assuming that everything that I was told in the tutorial by you guys is everything that I need to know about them. Clearly both assumptions are wrong. I didnt use saveIPFS in the cloud function, its in the create items function

the above code is not for a cloud function, is for normal web browser javascript code

And how do i use that object in the function? My experience is in bash, C, and a bit of html css and js. I dont kmow how to use that

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