metaplex-foundation / metaplex

A directory of what the Metaplex Foundation works on!
https://metaplex.com
Apache License 2.0
3.32k stars 6.26k forks source link

SyntaxError: Unexpected token n in JSON at position 4 #2303

Closed haotian-engineer closed 11 months ago

haotian-engineer commented 11 months ago

I am having above error while trying to upload metadata.

async function uploadMetadata(
  nftName: string,
  symbol: string,
  seller_fee_basis_points: number,
  attributes: { trait_type: string, value: string }[],
  collectionName: string,
  collectionFamily: string,
  imgUri: string,
) {
  const { uri } = await METAPLEX
    .nfts()
    .uploadMetadata({
      name: nftName,
      symbol: symbol,
      seller_fee_basis_points: seller_fee_basis_points,
      attributes: attributes,
      collection: {
        name: collectionName,
        family: collectionFamily
      },
      image: imgUri,
      properties: {
        files: [
          {
            type: "image/png",
            uri: imgUri,
          },
        ]
      }

    });

  return uri;
}

Even I set the .uploadMetadata() to empty JSON object, it throws the same error. How to fix it?

haotian-engineer commented 11 months ago

I finally fixed issue. The JS SDK I was using has been deprecated and so I have switched to a new one, umi. Below is updated code.

const uploadMetadata = async (data) => {
  const imageFile = fs.readFileSync(`./uploads/${data.imgName}.jpg`)
  const [imageUri] = await umi.uploader.upload([imageFile])
  const uri = await umi.uploader.uploadJson({
    name: data.imgName,
    symbol: data.symbol,
    description: data.description,
    sellerFeeBasisPoints: data.sellerFeeBasisPoints,
    attributes: data.attributes,
    collection: {
      name: data.collectionName,
      family: data.collectionFamily
    },
    creators: data.creators,
    image: imageUri
  });

  return uri
};

It works fine.