Ran cloud function getNft for user undefined

i tried to run the cloud function from browser after authenticate the user but it also does not allow to read the objects and returns the empty array


but its showing these params in payload

it should be able to read that data?

yes but its not
because its not verify the user from calling cloud function

can you make that query directly from the browser console and it works without issues?

it gives me this error but moralis is initialized and ruuning queries in my app.
image

this is a vanilla JS project or a react project?

I mean to run the query that should work in cloud code, not to run the cloud code function

its react code my code is here its dahboard component that successfully running the Moralis.Cloud.run(“getNft”,{tokenId:“1”}) in useEffect
import React, { useEffect, useState } from “react”;

import { useMoralis, useMoralisWeb3Api } from “react-moralis”;

import { Link } from “react-router-dom”;

import axios from “axios”;

import Moralis from “moralis”;

const Dashboard = () => {

const { user, logout } = useMoralis();

const Web3Api = useMoralisWeb3Api();

const [nfts, setNfts] = useState([]);

useEffect(() => {

console.log(user);

// setACL().then(response=> console.log(response));

Moralis.Cloud.run("getNft",{tokenId:"1"})

}, []);

const setACL = async()=>{

const nftData = Moralis.Object.extend("NftMeta");

const query = new Moralis.Query(nftData);

query.limit(1000);

query.equalTo("owner",user.attributes.accounts[0]);

const myData = await query.find();

console.log(myData);

console.log(myData.length);

for (const element of myData) {

  element.setACL(new Moralis.ACL(Moralis.User.current()))

  // console.log("Saving data");

  await element.save();

}

}

const callApi = async () => {

// const nfts = await Web3Api.Web3API.account.getNFTs({

//   chain: "rinkeby",

// });

// console.log(nfts.result.filter(item => item.token_address === "0x3689fedc16168407d631d2e77a88e67387536afd"));

// setNfts(nfts.result.filter(item => item.token_address === "0x3689fedc16168407d631d2e77a88e67387536afd"));



const nft = await axios.get(

  "https://nb0rbiaezyup.usemoralis.com:2053/server/functions/getNft?_ApplicationId=Egd4T5joZLFXPNyixev7xZCdrX27mymyOxrnVegE",

  { params: { tokenId: "29"} }

);

console.log(nft);

};

return (

<>

  <div className="text-center my-5">

    Welcome to the dashboard

    <br />

    <button className="btn btn-danger my-2" onClick={logout}>

      Logout

    </button>

    <br />

    <Link to="/editProfile">

      <button className="btn btn-outline-info my-2">Edit Profile</button>

    </Link>

    <br />

    <button className="btn btn-outline-success" onClick={callApi}>

      Call Api

    </button>

  </div>

  <div></div>

</>

);

};

export default Dashboard;

you can see here how to post code on forum:

import React, { useEffect, useState } from "react";

import { useMoralis, useMoralisWeb3Api } from "react-moralis";

import { Link } from "react-router-dom";

import axios from "axios";

import Moralis from "moralis";

const Dashboard = () => {

  const { user, logout } = useMoralis();

  const Web3Api = useMoralisWeb3Api();

  const [nfts, setNfts] = useState([]);

  useEffect(() => {

    console.log(user);

    // setACL().then(response=> console.log(response));

    Moralis.Cloud.run("getNft",{tokenId:"1"})

  }, []);

  const setACL = async()=>{

    const nftData = Moralis.Object.extend("NftMeta");

    const query = new Moralis.Query(nftData);

    query.limit(1000);

    query.equalTo("owner",user.attributes.accounts[0]);

    const myData = await query.find();

    console.log(myData);

    console.log(myData.length);

    for (const element of myData) {

      element.setACL(new Moralis.ACL(Moralis.User.current()))

      // console.log("Saving data");

      await element.save();

    }

  }

  const callApi = async () => {

    // const nfts = await Web3Api.Web3API.account.getNFTs({

    //   chain: "rinkeby",

    // });

    // console.log(nfts.result.filter(item => item.token_address === "0x3689fedc16168407d631d2e77a88e67387536afd"));

    // setNfts(nfts.result.filter(item => item.token_address === "0x3689fedc16168407d631d2e77a88e67387536afd"));

   

    const nft = await axios.get(

      "https://nb0rbiaezyup.usemoralis.com:2053/server/functions/getNft?_ApplicationId=Egd4T5joZLFXPNyixev7xZCdrX27mymyOxrnVegE",

      { params: { tokenId: "29"} }

    );

    console.log(nft);

  };

  return (

    <>

      <div className="text-center my-5">

        Welcome to the dashboard

        <br />

        <button className="btn btn-danger my-2" onClick={logout}>

          Logout

        </button>

        <br />

        <Link to="/editProfile">

          <button className="btn btn-outline-info my-2">Edit Profile</button>

        </Link>

        <br />

        <button className="btn btn-outline-success" onClick={callApi}>

          Call Api

        </button>

      </div>

      <div></div>

    </>

  );

};

export default Dashboard;

you may want to import moralis-v1 here, only for the future

where in particular I should look at that code?

my issue is still there How to access objects of database that have acl for particular user from postman or api call through axios

you need the session token to be able to do that, if you send the session token as a parameter to a cloud function, you can check the session token in session table, validate the user, and then use master key to read the data for that specific user

@cryptokid when i am trying to query from session table with the session token in request params it gives me error 209 invalid session token
can you please help this

How are you trying to do that query?

I wanted to say that you can read the session token from the parameters, and then search it in the session table with master key

@cryptokid here my api request of cloud function

const callApi = async () => {

    const nft = await axios.get(

      "https://nb0rbiaezyup.usemoralis.com:2053/server/functions/getNft?_ApplicationId=Egd4T5joZLFXPNyixev7xZCdrX27mymyOxrnVegE",

      { params: { tokenId:"29", sessionToken:user.attributes.sessionToken} }

    );

    console.log(nft);

  };

And here the query of finding session token from session table in cloud function

const sessionClass = Moralis.Object.extend("_Session");
    const sessionQuery = new Moralis.Query(sessionClass);
    sessionQuery.equalTo("sessionToken",data.sessionToken);
    const sessionObject = await sessionQuery.find();

in response it returns this error
image

I think that parse server tries to validate that session token. Try to add master key parameter, change the variable name for session token, add some debugging, check when user is authenticated and when it is not authenticated.

you can look on how the request is made in the browser (in network tab), and make the same request with curl or postman, it should work if it works in browser

Thanks it works by just changing the name of param sessionToken

1 Like