[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:

https://gateway.pinata.cloud/ipfs/QmWjdMFWUVGUw5fhoCHsvjN1a2HUmyHUvx6f1XcE8iChM3

I am getting an empty sub array for my attributes:

https://gateway.pinata.cloud/ipfs/QmbV5HaTDmmC1QfZeDR17aBR6qhNDn6AbemaN8izaJn5wb

I am creating my metadata as follows:

var metadata = {
  name: dataset.image[1],
  image: `${IPFSBaseUrl}${dataset.image[0]}`,
  tokenId: tokenId,
  description:
    "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:

Moralis.Cloud.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) {
	logger.info(httpResponse.text);
}, 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);

Moralis.Cloud.toIpfs({
   sourceType: "string",
   source: source
}).then(function(response) {
	logger.info(response.text);
}, 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