getItems [I CLONED RARABLE IN 24 HOURS pt13]

I am trying to load uid, askingPrice, symbol, etc. within my Network console like what is shown here:

Screenshot 2022-07-11 161119

Instead i get this in my network console:

and this in my console:

I believe that the issue is to do with loadItems and cloud function.

My cloud function is as follows:

Moralis.Cloud.define("getUserItems", async (request) => {

    const query = new Moralis.Query("NFTTokenOwners");
    query.equalTo("contract_type", "ERC721");
    query.containedIn("owner_of", request.user.attributes.accounts);
    const queryResults = await query.find();
    const results = [];
    for (let i = 0; i < queryResults.length; ++i) {
      results.push({
        "tokenObjectId": queryResults[i].id,
        "tokenId": queryResults[i].attributes.token_id,
        "tokenAddress": queryResults[i].attributes.token_address,
        "symbol": queryResults[i].attributes.symbol,
        "tokenUri": queryResults[i].attributes.token_uri,
      });
    }
    return results;
  });
  
  Moralis.Cloud.beforeSave("ItemsForSale", async (request) => {
  
    const query = new Moralis.Query("NFTTokenOwners");
    query.equalTo("token_address", request.object.get('tokenAddress'));
    query.equalTo("token_id", request.object.get('tokenId'));
    const object = await query.first();
    if (object){
        const owner = object.attributes.owner_of;
      const userQuery = new Moralis.Query(Moralis.User);
        userQuery.equalTo("accounts", owner);
      const userObject = await userQuery.first({useMasterKey:true});
      if (userObject){
          request.object.set('user', userObject);
      }
      request.object.set('token', object);
    }
  });
  
  Moralis.Cloud.beforeSave("SoldItems", async (request) => {
  
    const query = new Moralis.Query("ItemsForSale");
    query.equalTo("uid", request.object.get('uid'));
    const item = await query.first();
    if (item){
      request.object.set('item', item);
      item.set('isSold', true);
      await item.save();
      
    
      const userQuery = new Moralis.Query(Moralis.User);
        userQuery.equalTo("accounts", request.object.get('buyer'));
      const userObject = await userQuery.first({useMasterKey:true});
      if (userObject){
          request.object.set('user', userObject);
      }
    }
  });    
  
  Moralis.Cloud.define("getItems", async (request) => {
      
    const query = new Moralis.Query("ItemsForSale");
    query.notEqualTo("isSold", true);
    query.select("uid","askingPrice","tokenAddress","tokenId", "token.token_uri", "token.symbol","token.owner_of","token.id", "user.avatar","user.username");
  
    const queryResults = await query.find({useMasterKey:true});
    const results = [];
  
    for (let i = 0; i < queryResults.length; ++i) {
  
      if (!queryResults[i].attributes.token || !queryResults[i].attributes.user) continue;
  
      results.push({
        "uid": queryResults[i].attributes.uid,
        "tokenId": queryResults[i].attributes.tokenId,
        "tokenAddress": queryResults[i].attributes.tokenAddress,
        "askingPrice": queryResults[i].attributes.askingPrice,
  
        "symbol": queryResults[i].attributes.token.attributes.symbol,
        "tokenUri": queryResults[i].attributes.token.attributes.token_uri,
        "ownerOf": queryResults[i].attributes.token.attributes.owner_of,
        "tokenObjectId": queryResults[i].attributes.token.id,
        
        "sellerUsername": queryResults[i].attributes.user.attributes.username,
        "sellerAvatar": queryResults[i].attributes.user.attributes.avatar,
      });
    }
  
    return results;
  });
  
  Moralis.Cloud.define("getItem", async (request) => {
      
    const query = new Moralis.Query("ItemsForSale");
    query.equalTo("uid", request.params.uid);
    query.select("uid","askingPrice","tokenAddress","tokenId", "token.token_uri", "token.symbol","token.owner_of","token.id","user.avatar","user.username");
  
    const queryResult = await query.first({useMasterKey:true});
    if (!queryResult) return;
    return {
        "uid": queryResult.attributes.uid,
        "tokenId": queryResult.attributes.tokenId,
        "tokenAddress": queryResult.attributes.tokenAddress,
        "askingPrice": queryResult.attributes.askingPrice,
  
        "symbol": queryResult.attributes.token.attributes.symbol,
        "tokenUri": queryResult.attributes.token.attributes.token_uri,
        "ownerOf": queryResult.attributes.token.attributes.owner_of,
        "tokenObjectId": queryResult.attributes.token.id,
  
        "sellerUsername": queryResult.attributes.user.attributes.username,
        "sellerAvatar": queryResult.attributes.user.attributes.avatar,
      };
  });

loadItems function:

loadItems = async () => {
    const params =  { token_address: TOKEN_CONTRACT_ADDRESS };
    const items = await Moralis.Cloud.run("getItems", params);
    user = await Moralis.User.current();
    items.forEach(item => {
        if(user){
            if(user.attributes.accounts.includes(item.ownerOf)) return;
        }
        getAndRenderItemData(item, renderItem);
    });
}

renderItem function:

renderItem = (item) => {
    const itemForSale = marketplaceItemTemplate.cloneNode(true);
    if (item.avatar){
        itemForSale.getElementsByTagName("img")[0].src = item.sellerAvatar.url();
        itemForSale.getElementsByTagName("img")[0].alt = item.sellerUsername;
        //itemForSale.getElementsByTagName("span")[0].innerText = item.sellerUsername;
    }
    itemForSale.getElementsByTagName("img")[1].src = item.image;
    itemForSale.getElementsByTagName("img")[1].alt = item.name;
    itemForSale.getElementsByTagName("h5")[0].innerText = item.name;
    itemForSale.getElementsByTagName("p")[0].innerText = item.description;

    itemForSale.getElementsByTagName("button")[0].innerText = `Buy for ${item.askingPrice}`;
    //itemForSale.getElementsByTagName("button")[0].onclick = ()  => buyItem(item);;
    itemForSale.id = `item-${item.uid}`;
    itemsForSale.appendChild(itemForSale);
}

Is there any data in the ItemsForSale class that has isSold value as false?

When i create an item, the item lists initially as false but changes to true under ‘confirmed’. All items are true now.

Then, that’s the reason why the result is empty from getItems.

1 Like

https://hiee58acp8ql.usemoralis.com:2053/server/functions/getItems shows {"result":[]} which means it has to do with the cloud function at getItems.
Which is here:

 Moralis.Cloud.define("getItems", async (request) => {
   const query = new Moralis.Query("ItemsForSale");
query.notEqualTo("isSold", true);
query.select("uid","askingPrice","tokenAddress","tokenId", "token.token_uri", "token.symbol","token.owner_of","token.id", "user.avatar","user.username");

const queryResults = await query.find({useMasterKey:true});
const results = [];

for (let i = 0; i < queryResults.length; ++i) {

  if (!queryResults[i].attributes.token || !queryResults[i].attributes.user) continue;

  results.push({
    "uid": queryResults[i].attributes.uid,
    "tokenId": queryResults[i].attributes.tokenId,
    "tokenAddress": queryResults[i].attributes.tokenAddress,
    "askingPrice": queryResults[i].attributes.askingPrice,

    "symbol": queryResults[i].attributes.token.attributes.symbol,
    "tokenUri": queryResults[i].attributes.token.attributes.token_uri,
    "ownerOf": queryResults[i].attributes.token.attributes.owner_of,
    "tokenObjectId": queryResults[i].attributes.token.id,
    
    "sellerUsername": queryResults[i].attributes.user.attributes.username,
    "sellerAvatar": queryResults[i].attributes.user.attributes.avatar,
  });
}

which in turn, communicates with:

loadItems = async () => {
    const params =  { token_address: TOKEN_CONTRACT_ADDRESS };
    const items = await Moralis.Cloud.run("getItems", params);
    user = await Moralis.User.current();
    items.forEach(item => {
        if(user){
            if(user.attributes.accounts.includes(item.ownerOf)) {
                const userItemListing = document.getElementById(`user-item-${item.tokenObjectId}`);
                if (userItemListing) userItemListing.parentNode.removeChild(userItemListing);
                //getAndRenderItemData(item, renderUserItem);
                return;
            }
        }
        getAndRenderItemData(item, renderItem);
    });
}

How would i update this to fit Moralis now?

Ive changed my Cloud Function to this:

Moralis.Cloud.define("getItems", async (request) => {
      
    const query = new Moralis.Query("ItemsForSale");
    query.notEqualTo("isSold", true);

   if (request.user){
     query.notContainedIn("token.owner_of", request.user.attributes.accounts);
   }
   query.select("uid","askingPrice","tokenAddress","tokenId", "token.token_uri", "token.symbol","token.owner_of","token.id", "user.avatar","user.username");

    const queryResults = await query.find({useMasterKey:true});
    const results = [];
  
    for (let i = 0; i < queryResults.length; ++i) {
  
      if (!queryResults[i].attributes.token || !queryResults[i].attributes.user) continue;
  
      results.push({
        "uid": queryResults[i].attributes.uid,
        "tokenId": queryResults[i].attributes.tokenId,
        "tokenAddress": queryResults[i].attributes.tokenAddress,
        "askingPrice": queryResults[i].attributes.askingPrice,
  
        "symbol": queryResults[i].attributes.token.symbol,
        "tokenUri": queryResults[i].attributes.token.token_uri,
        "ownerOf": queryResults[i].attributes.token.owner_of,
        "tokenObjectId": queryResults[i].attributes.token.id,
        
        "sellerUsername": queryResults[i].attributes.user.username,
        "sellerAvatar": queryResults[i].attributes.user.avatar,
      });
    }
  
    return results;
  });

I can now display the result from the array but it is still empty:

Screenshot 2022-07-12 113445

Check if you’re getting any results from your query. Are you meant to be getting a result (manually verify it by looking at your ItemsForSale class and checking against your parameters)?

You can use logger.info(variable) and check your server logs.

This is my entire server error log:

2022-07-12T01:58:25.765Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:58:25.762Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0x08257e23a7cfb6a02da0876d7effc5ce010b167a23593bdc9839e3acdc9d12c2”,“log_index”:11,“uid”:“23”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“23”},“tokenId”:“46”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“46”},“tokenAddress”:“0x0de601e38e2282aede397bf5c16b819000961301”,“askingPrice”:“599999999999999”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“599999999999999”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0xa69219d597675f41e30413fd58dac5b2d3f21fb71cd1b731f5d2872eaa954306”,“block_number”:12574984,“transaction_index”:2,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:58:24.000Z”},“confirmed”:false}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:58:25.509Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:58:25.506Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0x08257e23a7cfb6a02da0876d7effc5ce010b167a23593bdc9839e3acdc9d12c2”,“log_index”:11,“uid”:“23”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“23”},“tokenId”:“46”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“46”},“tokenAddress”:“0x0de601e38e2282aede397bf5c16b819000961301”,“askingPrice”:“599999999999999”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“599999999999999”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0xa69219d597675f41e30413fd58dac5b2d3f21fb71cd1b731f5d2872eaa954306”,“block_number”:12574984,“transaction_index”:2,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:58:24.000Z”},“confirmed”:false}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:58:02.528Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:58:02.524Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0x1ff99a72bac336fd41801e5cc13bdbdaec086532ae493dd738af60293a651eea”,“log_index”:17,“uid”:“22”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“22”},“tokenId”:“187”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“187”},“tokenAddress”:“0x99495223da7dfe1a14d5224bc90bad4a01f0e58a”,“askingPrice”:“191919191919191”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“191919191919191”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0x11bf148112985b92b9104bed0edf8c9f7e8025cba99d5fbaca7a770e31dabdef”,“block_number”:12574982,“transaction_index”:5,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:58:00.000Z”},“confirmed”:false}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:58:02.280Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:58:02.274Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0x1ff99a72bac336fd41801e5cc13bdbdaec086532ae493dd738af60293a651eea”,“log_index”:17,“uid”:“22”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“22”},“tokenId”:“187”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“187”},“tokenAddress”:“0x99495223da7dfe1a14d5224bc90bad4a01f0e58a”,“askingPrice”:“191919191919191”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“191919191919191”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0x11bf148112985b92b9104bed0edf8c9f7e8025cba99d5fbaca7a770e31dabdef”,“block_number”:12574982,“transaction_index”:5,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:58:00.000Z”},“confirmed”:false}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:50:14.199Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:50:14.196Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0xe52a3743b9e890f158b375e35bbb89bc22fccbb3edc99f00902096e024b3cddd”,“log_index”:0,“uid”:“21”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“21”},“tokenId”:“44”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“44”},“tokenAddress”:“0x0de601e38e2282aede397bf5c16b819000961301”,“askingPrice”:“22222222222222222222222”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“22222222222222222222222”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0xdc8eb64dae118d88ddecb626f1bbb7bcaf5198ec9f637cb690eefb2bd02d6007”,“block_number”:12574939,“transaction_index”:0,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:47:36.000Z”},“confirmed”:true}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:50:13.943Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:50:13.939Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0xe52a3743b9e890f158b375e35bbb89bc22fccbb3edc99f00902096e024b3cddd”,“log_index”:0,“uid”:“21”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“21”},“tokenId”:“44”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“44”},“tokenAddress”:“0x0de601e38e2282aede397bf5c16b819000961301”,“askingPrice”:“22222222222222222222222”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“22222222222222222222222”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0xdc8eb64dae118d88ddecb626f1bbb7bcaf5198ec9f637cb690eefb2bd02d6007”,“block_number”:12574939,“transaction_index”:0,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:47:36.000Z”},“confirmed”:true}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:47:37.173Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:47:37.170Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0xe52a3743b9e890f158b375e35bbb89bc22fccbb3edc99f00902096e024b3cddd”,“log_index”:0,“uid”:“21”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“21”},“tokenId”:“44”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“44”},“tokenAddress”:“0x0de601e38e2282aede397bf5c16b819000961301”,“askingPrice”:“22222222222222222222222”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“22222222222222222222222”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0xdc8eb64dae118d88ddecb626f1bbb7bcaf5198ec9f637cb690eefb2bd02d6007”,“block_number”:12574939,“transaction_index”:0,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:47:36.000Z”},“confirmed”:false}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:47:36.926Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:47:36.915Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0xe52a3743b9e890f158b375e35bbb89bc22fccbb3edc99f00902096e024b3cddd”,“log_index”:0,“uid”:“21”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“21”},“tokenId”:“44”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“44”},“tokenAddress”:“0x0de601e38e2282aede397bf5c16b819000961301”,“askingPrice”:“22222222222222222222222”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“22222222222222222222222”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0xdc8eb64dae118d88ddecb626f1bbb7bcaf5198ec9f637cb690eefb2bd02d6007”,“block_number”:12574939,“transaction_index”:0,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:47:36.000Z”},“confirmed”:false}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:32:57.988Z - Error: Signing message has expired.
at /moralis-server/lib/Adapters/Auth/moralisEth.js:48:13
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Promise.all (index 0)
2022-07-12T01:31:55.394Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:31:55.390Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0xe747f063d5d5caa432de96b7ebb4339d33f45b0be0e8b82696edbfbc805543a0”,“log_index”:0,“uid”:“20”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“20”},“tokenId”:“186”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“186”},“tokenAddress”:“0x99495223da7dfe1a14d5224bc90bad4a01f0e58a”,“askingPrice”:“191919191919191”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“191919191919191”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0x31af5193ab7fb6a2393674d623e050fbfb50d9647e8b000f093f48daa81116cc”,“block_number”:12574857,“transaction_index”:0,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:28:24.000Z”},“confirmed”:true}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:31:55.138Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:31:55.133Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0xe747f063d5d5caa432de96b7ebb4339d33f45b0be0e8b82696edbfbc805543a0”,“log_index”:0,“uid”:“20”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“20”},“tokenId”:“186”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“186”},“tokenAddress”:“0x99495223da7dfe1a14d5224bc90bad4a01f0e58a”,“askingPrice”:“191919191919191”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“191919191919191”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0x31af5193ab7fb6a2393674d623e050fbfb50d9647e8b000f093f48daa81116cc”,“block_number”:12574857,“transaction_index”:0,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:28:24.000Z”},“confirmed”:true}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:31:44.042Z - Error: Invalid function: “coreservices_getConfig”
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:201:13)
at /moralis-server/lib/PromiseRouter.js:85:20
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:28:57.991Z - TypeError: query.select.notContainedIn is not a function
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:1899)
at /moralis-server/lib/Routers/FunctionsRouter.js:264:16
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:28:57.987Z - Failed running cloud function getItems for user EYS982t8rd6ZAoQhFPRXjAKC with:
Input: {“token_address”:“0x0dE601E38e2282AEDE397BF5c16B819000961301”}
Error: {“message”:“query.select.notContainedIn is not a function”,“code”:141}
2022-07-12T01:28:25.011Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:28:25.008Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0xe747f063d5d5caa432de96b7ebb4339d33f45b0be0e8b82696edbfbc805543a0”,“log_index”:0,“uid”:“20”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“20”},“tokenId”:“186”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“186”},“tokenAddress”:“0x99495223da7dfe1a14d5224bc90bad4a01f0e58a”,“askingPrice”:“191919191919191”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“191919191919191”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0x31af5193ab7fb6a2393674d623e050fbfb50d9647e8b000f093f48daa81116cc”,“block_number”:12574857,“transaction_index”:0,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:28:24.000Z”},“confirmed”:false}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:28:24.741Z - ReferenceError: query is not defined
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:861)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:28:24.736Z - beforeSave failed for ItemsForSale for user np86HSDpg9hK4noLGlHtLF5T:
Input: {“transaction_hash”:“0xe747f063d5d5caa432de96b7ebb4339d33f45b0be0e8b82696edbfbc805543a0”,“log_index”:0,“uid”:“20”,“uid_decimal”:{"__type":“NumberDecimal”,“value”:“20”},“tokenId”:“186”,“tokenId_decimal”:{"__type":“NumberDecimal”,“value”:“186”},“tokenAddress”:“0x99495223da7dfe1a14d5224bc90bad4a01f0e58a”,“askingPrice”:“191919191919191”,“askingPrice_decimal”:{"__type":“NumberDecimal”,“value”:“191919191919191”},“address”:“0x5b71c0259d1cfa36dd71a2c308e5a6378db5977f”,“block_hash”:“0x31af5193ab7fb6a2393674d623e050fbfb50d9647e8b000f093f48daa81116cc”,“block_number”:12574857,“transaction_index”:0,“block_timestamp”:{"__type":“Date”,“iso”:“2022-07-12T01:28:24.000Z”},“confirmed”:false}
Error: {“message”:“query is not defined”,“code”:141}
2022-07-12T01:23:58.530Z - TypeError: Cannot read properties of undefined (reading ‘accounts’)
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), :1:1954)
at /moralis-server/lib/Routers/FunctionsRouter.js:264:16
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:23:58.525Z - Failed running cloud function getItems for user EYS982t8rd6ZAoQhFPRXjAKC with:
Input: {“token_address”:“0x0dE601E38e2282AEDE397BF5c16B819000961301”}
Error: {“message”:“Cannot read properties of undefined (reading ‘accounts’)”,“code”:141}
2022-07-12T01:18:46.677Z - Error: Invalid function: “pluginsReady”
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:201:13)
at /moralis-server/lib/PromiseRouter.js:85:20
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:18:46.602Z - Error: Invalid function: “coreservices_getConfig”
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:201:13)
at /moralis-server/lib/PromiseRouter.js:85:20
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T01:07:00.452Z - Error: Invalid function: “pluginsReady”
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:201:13)
at /moralis-server/lib/PromiseRouter.js:85:20
at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-07-12T00:48:07.668Z - Error: Invalid function: “getItems”
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:201:13)
at /moralis-server/lib/PromiseRouter.js:85:20
2022-07-12T00:46:25.636Z - SyntaxError: Unexpected identifier
at customUserPlugin (/moralis-server/cloud/main.js:157:26)
at /moralis-server/lib/cloud-code/plugins/index.js:144:15
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.initialize (/moralis-server/lib/cloud-code/plugins/index.js:133:3)
2022-07-12T00:46:25.635Z - CLOUD FUNCTION ERROR PLEASE CHECK YOUR CLOUD FUNCTION CODE
2022-07-12T00:42:41.711Z - SyntaxError: Unexpected identifier
at customUserPlugin (/moralis-server/cloud/main.js:157:26)
at /moralis-server/lib/cloud-code/plugins/index.js:144:15
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.initialize (/moralis-server/lib/cloud-code/plugins/index.js:133:3)
2022-07-12T00:42:41.710Z - CLOUD FUNCTION ERROR PLEASE CHECK YOUR CLOUD FUNCTION CODE

It looks like you’re getting errors in your Moralis.Cloud.beforeSave for “ItemsForSale”, it is saying query is not defined. Have you made changes to it compared to what you posted above?

this is my entire current Cloud Function:

Moralis.Cloud.define("getUserItems", async (request) => {

  	const options = { address: request.params.token_address, chain: "ropsten" };
  	const NFTowners = await Moralis.Web3API.token.getNFTOwners(options);
  	const queryResults = NFTowners.result.filter(x => x.owner_of == request.user.attributes.accounts);
    const results = [];
 	for (let i = 0; i < queryResults.length; ++i) {
      results.push({
        "tokenObjectId": queryResults[i].id,
        "tokenid": queryResults[i].token_id,
        "tokenAddress": queryResults[i].token_address,
        "symbol": queryResults[i].symbol,
        "tokenUri": queryResults[i].token_uri,
      });
    }
    return results;
  });

Moralis.Cloud.beforeSave("ItemsForSale", async (request) => {
  
    const options = { address: "0x0dE601E38e2282AEDE397BF5c16B819000961301", chain: "ropsten" };
  	const NFTowners = await Moralis.Web3API.token.getNFTOwners(options);
    const queryResults = NFTowners.result.filter(x => x.token_address == request.object.get('tokenAddress') && x.token_id == request.object.get('tokenId'));
    //query.equalTo("token_address", request.object.get('tokenAddress'));
    //query.equalTo("token_id", request.object.get('tokenId'));
    const object = queryResults[0];
    if (object){
    	const owner = object.owner_of;
        const userQuery = new Moralis.Query(Moralis.User);
        userQuery.equalTo("accounts", owner);
        const userObject = await userQuery.first({useMasterKey:true});
        if (userObject){
            request.object.set('user', userObject);
        }
      	//request.object.set('token', object);
    }
  });

Moralis.Cloud.beforeSave("SoldItems", async (request) => {
  
    const query = new Moralis.Query("ItemsForSale");
    query.equalTo("uid", request.object.get('uid'));
    const item = await query.first();
    if (item){
      request.object.set('item', item);
      item.set('isSold', true);
      await item.save();
      
    
      const userQuery = new Moralis.Query(Moralis.User);
        userQuery.equalTo("accounts", request.object.get('buyer'));
      const userObject = await userQuery.first({useMasterKey:true});
      if (userObject){
          request.object.set('user', userObject);
      }
    }
  });    

Moralis.Cloud.define("getItems", async (request) => {
      
    const query = new Moralis.Query("ItemsForSale");
    query.notEqualTo("isSold", true);

   if (request.user){
     query.notContainedIn("token.owner_of", request.user.attributes.accounts);
   }
   query.select("uid","askingPrice","tokenAddress","tokenId", "token.token_uri", "token.symbol","token.owner_of","token.id", "user.avatar","user.username");

    const queryResults = await query.find({useMasterKey:true});
    const results = [];
  
    for (let i = 0; i < queryResults.length; ++i) {
  
      if (!queryResults[i].attributes.token || !queryResults[i].attributes.user) continue;
  
      results.push({
        "uid": queryResults[i].attributes.uid,
        "tokenId": queryResults[i].attributes.tokenId,
        "tokenAddress": queryResults[i].attributes.tokenAddress,
        "askingPrice": queryResults[i].attributes.askingPrice,
  
        "symbol": queryResults[i].attributes.token.symbol,
        "tokenUri": queryResults[i].attributes.token.token_uri,
        "ownerOf": queryResults[i].attributes.token.owner_of,
        "tokenObjectId": queryResults[i].attributes.token.id,
        
        "sellerUsername": queryResults[i].attributes.user.username,
        "sellerAvatar": queryResults[i].attributes.user.avatar,
      });
    }
  
    return results;
  });

  Moralis.Cloud.define("getItem", async (request) => {
      
    const query = new Moralis.Query("ItemsForSale");
    query.equalTo("uid", request.params.uid);
    query.select("uid", "tokenAddress", "tokenId", "askingPrice", "token.token_uri", "token.symbol", "token.owner_of", "user.username", "user.avatar");
  
    const queryResult = await query.first({useMasterKey:true});
    if (!queryResult) return;
    return {
        "uid": queryResult.attributes.uid,
        "tokenId": queryResult.attributes.tokenId,
        "tokenAddress": queryResult.attributes.tokenAddress,
        "askingPrice": queryResult.attributes.askingPrice,
  
        "symbol": queryResult.attributes.token.attributes.symbol,
        "tokenUri": queryResult.attributes.token.attributes.token_uri,
        "ownerOf": queryResult.attributes.token.attributes.owner_of,
        "tokenObjectId": queryResult.attributes.token.id,
  
        "sellerUsername": queryResult.attributes.user.attributes.username,
        "sellerAvatar": queryResult.attributes.user.attributes.avatar,
      };
  });

I’ve somehow brought back the User pointer to ItemsForSale table has broken again.
Regardless, im just completely stuck on previewing the results on network.

I have fixed some other issues I’ve been having, such as User and Token pointer within the ItemsForSale table.

The getItems result array becomes populated, which is great but now Im getting this error:

Followed with:

This is my new Cloud Function:

Moralis.Cloud.define("getUserItems", async (request) => {

  	const options = { address: request.params.token_address, chain: "ropsten" };
  	const NFTowners = await Moralis.Web3API.token.getNFTOwners(options);
  	const queryResults = NFTowners.result.filter(x => x.owner_of == request.user.attributes.accounts);
    const results = [];
 	for (let i = 0; i < queryResults.length; ++i) {
      results.push({
        "tokenObjectId": queryResults[i].id,
        "tokenid": queryResults[i].token_id,
        "tokenAddress": queryResults[i].token_address,
        "symbol": queryResults[i].symbol,
        "tokenUri": queryResults[i].token_uri,
      });
    }
    return results;
  });

Moralis.Cloud.beforeSave("ItemsForSale", async (request) => {
  
    const query = new Moralis.Query("EthNFTTransfers");
  	query.equalTo("token_address", request.object.get('tokenAddress'));
    query.equalTo("token_id", request.object.get('tokenId'));
    const object = await query.first();
    if (object){
    	const owner = object.attributes.owner_of;
        const userQuery = new Moralis.Query(Moralis.User);	
        userQuery.equalTo("accounts", owner);
        const userObject = await userQuery.first({useMasterKey:true});
        if (userObject){
            request.object.set('user', userObject);
        }
      	request.object.set('token', object);
    }
  });

Moralis.Cloud.beforeSave("SoldItems", async (request) => {
  
    const query = new Moralis.Query("ItemsForSale");
    query.equalTo("uid", request.object.get('uid'));
    const item = await query.first();
    if (item){
      request.object.set('item', item);
      item.set('isSold', true);
      await item.save();
      
    
      const userQuery = new Moralis.Query(Moralis.User);
        userQuery.equalTo("accounts", request.object.get('buyer'));
      const userObject = await userQuery.first({useMasterKey:true});
      if (userObject){
          request.object.set('user', userObject);
      }
    }
  });    

 Moralis.Cloud.define("getItems", async (request) => {
      
    const query = new Moralis.Query("ItemsForSale");
    query.notEqualTo("isSold", true);
    query.select("uid","askingPrice","tokenAddress","tokenId", "token.token_uri", "token.symbol","token.owner_of","token.id", "user.avatar","user.username");
  
    const queryResults = await query.find({useMasterKey:true});
    const results = [];
  
    for (let i = 0; i < queryResults.length; ++i) {
  
      if (!queryResults[i].attributes.token || !queryResults[i].attributes.user) continue;
  
      results.push({
        "uid": queryResults[i].attributes.uid,
        "tokenId": queryResults[i].attributes.tokenId,
        "tokenAddress": queryResults[i].attributes.tokenAddress,
        "askingPrice": queryResults[i].attributes.askingPrice,
  
        "symbol": queryResults[i].attributes.token.symbol,
        "tokenUri": queryResults[i].attributes.token.token_uri,
        "ownerOf": queryResults[i].attributes.token.owner_of,
        "tokenObjectId": queryResults[i].attributes.token.id,
        
        "sellerUsername": queryResults[i].attributes.user.username,
        "sellerAvatar": queryResults[i].attributes.user.avatar,
      });
    }
  
    return results;
  });







  Moralis.Cloud.define("getItem", async (request) => {
      
    const query = new Moralis.Query("ItemsForSale");
    query.equalTo("uid", request.params.uid);
    query.select("uid","askingPrice","tokenAddress","tokenId", "token.token_uri", "token.symbol","token.owner_of","token.id","user.avatar","user.username");
  
    const queryResult = await query.first({useMasterKey:true});
    if (!queryResult) return;
    return {
        "uid": queryResult.attributes.uid,
        "tokenId": queryResult.attributes.tokenId,
        "tokenAddress": queryResult.attributes.tokenAddress,
        "askingPrice": queryResult.attributes.askingPrice,
  
        "symbol": queryResult.attributes.token.attributes.symbol,
        "tokenUri": queryResult.attributes.token.attributes.token_uri,
        "ownerOf": queryResult.attributes.token.attributes.owner_of,
        "tokenObjectId": queryResult.attributes.token.id,
  
        "sellerUsername": queryResult.attributes.user.attributes.username,
        "sellerAvatar": queryResult.attributes.user.attributes.avatar,
      };
  });

First error is happening at main.js 224 line. Can you show the function/code which is in this location?

getAndRenderItemData = (item, renderFunction) => {

    fetch(item.tokenUri)

    .then(response => response.json())
//Line 224 \/
    .then(data => {

        item.name = data.name;

        item.description = data.description;

        item.image = data.image;

        renderFunction(item);

    })

}

Disregard the other reply, for some reason, i am now getting these errors now:

main.js:

getAndRenderItemData = (item, renderFunction) => {
// Line 222 \/
    fetch(item.tokenUri)
    .then(response => response.json())
    .then(data => {
        item.name = data.name;
        item.description = data.description;
        item.image = data.image;
        renderFunction(item);
    })
}

The Unexpected token < in JSON error means the response is not in JSON format - for example this can happen if you’re fetching a link that doesn’t resolve (and the response returns some HTML). What is item.tokenUri in this example? It seems like it is not reachable.

I thought it would have been my build/contracts so i removed and migrate truffle -- reset. This didnt seem to work, so i went into the cloud function and realised that i was missing something in the getItems request.

I had this:

        "symbol": queryResult.attributes.token.symbol,
        "tokenUri": queryResult.attributes.token.token_uri,
        "ownerOf": queryResult.attributes.token.owner_of,
        "tokenObjectId": queryResult.attributes.token.id,

But needed this:

        "symbol": queryResult.attributes.token.attributes.symbol,
        "tokenUri": queryResult.attributes.token.attributes.token_uri,
        "ownerOf": queryResult.attributes.token.attributes.owner_of,
        "tokenObjectId": queryResult.attributes.token.id,

Now im getting these errors:

Line 171:

loadItems = async () => {
//line 171 \/
    const items = await Moralis.Cloud.run("getItems");
    user = await Moralis.User.current();
    items.forEach(item => {
        if (user){
            if (user.attributes.accounts.includes(item.ownerOf)){
                const userItemListing = document.getElementById(`user-item-${item.tokenObjectId}`);
                if (userItemListing) userItemListing.parentNode.removeChild(userItemListing);
                getAndRenderItemData(item, renderUserItem);
                return;
            }
        }
        getAndRenderItemData(item, renderItem);
    });
}

My full getItems cloud function:

Moralis.Cloud.define("getItems", async (request) => {
      
    const query = new Moralis.Query("ItemsForSale");
    query.notEqualTo("isSold", true);
    query.select("uid","askingPrice","tokenAddress","tokenId", "token.token_uri", "token.symbol","token.owner_of","token.id", "user.avatar","user.username");
  
    const queryResults = await query.find({useMasterKey:true});
    const results = [];
  
    for (let i = 0; i < queryResults.length; ++i) {
  
      if (!queryResults[i].attributes.token || !queryResults[i].attributes.user) continue;
  
      results.push({
        "uid": queryResults[i].attributes.uid,
        "tokenId": queryResults[i].attributes.tokenId,
        "tokenAddress": queryResults[i].attributes.tokenAddress,
        "askingPrice": queryResults[i].attributes.askingPrice,
  
        "symbol": queryResult.attributes.token.attributes.symbol,
        "tokenUri": queryResult.attributes.token.attributes.token_uri,
        "ownerOf": queryResult.attributes.token.attributes.owner_of,
        "tokenObjectId": queryResult.attributes.token.id,
        
        "sellerUsername": queryResults[i].attributes.user.username,
        "sellerAvatar": queryResults[i].attributes.user.avatar,
      });
    }
  
    return results;
  });

This error is from the dashboard log

These should be queryResults[i] like the rest.