[SOLVED] Getting Empty sub array when trying to pin to IPFS

I am trying to pin my Metadata using Cloud Functions, but while I expected to get something like this:


I am getting an empty sub array for my attributes:


I am creating my metadata as follows:

var metadata = {
  name: dataset.image[1],
  image: `${IPFSBaseUrl}${dataset.image[0]}`,
  tokenId: tokenId,
    "Planet Horse first generation horse. This is a horse that you can trade, race and breed. This is a first edition collectible and shows you are an early supporter of the Planet Horse Family!",
  attributes: [
      trait_type: "Rarity",
      value: dataset.rarity,
      trait_type: "Energy",
      value: dataset.energy,
      trait_type: "Speed",
      value: dataset.speed,
      max_value: 24,
      trait_type: "Susten",
      value: dataset.susten,
      max_value: 24,
      trait_type: "Power",
      value: dataset.power,
      max_value: 24,
      trait_type: "Sprint",
      value: dataset.sprint,
      max_value: 24,

And pining to IPFS using an httpRequest:

	method: 'POST',
  	url: 'https://api.pinata.cloud/pinning/pinJSONToIPFS',
  	headers: {
        'pinata_api_key': MY_API_KEY,
        'pinata_secret_api_key': MY_SECRET_KEY,
  	body: metadata
}).then(function(httpResponse) {
}, function(httpResponse) {
	logger.error('Request failed with response code' + httpResponse.status);

Canโ€™t figure out what I am doing wrong.

The keys in the example you posted (how you want it to look) are strings, maybe that is the issue?

you may need to convert metadata to string using JSON.stringify for example.

Unfortunately, converting the metadata do string will cause a 400 error since the pinata endpoint I am using (โ€˜https://api.pinata.cloud/pinning/pinJSONToIPFSโ€™) will only handle JSON content.

I am trying to use an alternative with Moralis.Cloud.toIpfs, however, I canโ€™t get the pined file URI because the result always shows as โ€˜undefinedโ€™, I am using the code:

source = JSON.stringify(metadata);

   sourceType: "string",
   source: source
}).then(function(response) {
}, function(response) {
	logger.error('Request failed with response code' + response.status);

Try to use logger.info(JSON.stringify(response))

Worked like a charm! Thanks @cryptokid

1 Like