Failed to save object: path /storage/kittyItemsCollection in account XXX already stores an object

[Error Code: 1101] cadence runtime error Execution failed:
error: failed to save object: path /storage/kittyItemsCollection in account 0x84a7ac3a26cde3cb already stores an object
→ 347729776bcf4dfdc347c1ad202c2827d7999a381426bac73b271c696b39c40a:46:10
|
46 | acct.save(<-kittyItems.createEmptyCollection(), to: kittyItems.CollectionStoragePath)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Below is my code

import { transaction, limit, proposer, payer, authorizations, authz, cdc } from "@onflow/fcl"
import { invariant } from '@onflow/util-invariant';
import { tx } from './util/tx';

const CODE = cdc`
  import FungibleToken from 0xFungibleToken
  import NonFungibleToken from 0xNonFungibleToken
  import FUSD from 0xFUSD
  import Momentables from 0xMomentables
  import NFTStorefront from 0xNFTStorefront

  pub fun hasFUSD(_ address: Address): Bool {
    let receiver = getAccount(address)
      .getCapability<&FUSD.Vault{FungibleToken.Receiver}>(/public/fusdReceiver)
      .check()

    let balance = getAccount(address)
      .getCapability<&FUSD.Vault{FungibleToken.Balance}>(/public/fusdBalance)
      .check()

    return receiver && balance`Preformatted text`
  }

  pub fun hasItems(_ address: Address): Bool {
    return getAccount(address)
      .getCapability<&Momentables.Collection{NonFungibleToken.CollectionPublic,     Momentables.MomentablesCollectionPublic}>(Momentables.CollectionPublicPath)
      .check()
  }

 pub fun hasStorefront(_ address: Address): Bool {
    return getAccount(address)
      .getCapability<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(NFTStorefront.StorefrontPublicPath)
      .check()
  }

  transaction {
   prepare(acct: AuthAccount) {
      if !hasFUSD(acct.address) {
        if acct.borrow<&FUSD.Vault>(from: /storage/fusdVault) == nil {
          acct.save(<-FUSD.createEmptyVault(), to: /storage/fusdVault)
        }
        acct.unlink(/public/fusdReceiver)
        acct.unlink(/public/fusdBalance)
        acct.link<&FUSD.Vault{FungibleToken.Receiver}>(/public/fusdReceiver, target: /storage/fusdVault)
        acct.link<&FUSD.Vault{FungibleToken.Balance}>(/public/fusdBalance, target: /storage/fusdVault)
      }

  if !hasItems(acct.address) {
    if acct.borrow<&Momentables.Collection>(from: Momentables.CollectionStoragePath) == nil {
      acct.save(<-Momentables.createEmptyCollection(), to: Momentables.CollectionStoragePath)
    }
    acct.unlink(Momentables.CollectionPublicPath)
    acct.link<&Momentables.Collection{NonFungibleToken.CollectionPublic, Momentables.MomentablesCollectionPublic}>(Momentables.CollectionPublicPath, target: Momentables.CollectionStoragePath)
  }

  if !hasStorefront(acct.address) {
    if acct.borrow<&NFTStorefront.Storefront>(from: NFTStorefront.StorefrontStoragePath) == nil {
      acct.save(<-NFTStorefront.createStorefront(), to: NFTStorefront.StorefrontStoragePath)
    }
    acct.unlink(NFTStorefront.StorefrontPublicPath)
    acct.link<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(NFTStorefront.StorefrontPublicPath, target: NFTStorefront.StorefrontStoragePath)
  }
}
  }
`;

export async function initializeAccount(address, opts = {}) {
  invariant(address != null, "Tried to initialize an account but no address was supplied")
  return tx(
    [
      transaction(CODE),
     limit(70),
      proposer(authz),
      payer(authz),
      authorizations([authz]),
    ],
    opts
  );
}
1 Like

hi @veerdev, @bastian do you have any idea of what could be causing this error?

The save function does not allow overwriting. You need to first remove the value from storage, using load.

@turbolent Thanks for the idea