Cloud function fails at requireUser Error: Validation failed. [SOLVED]

Hi, I have a react-moralis app with the following function:

function setNFTAsActive(id, user) {
        fetch("SERVERURL/server/functions/setActiveFighter?_ApplicationId=APPID&nftID=" + id + "&userid=" + user.id);
    }

and a following cloud function:

Moralis.Cloud.define("setActiveFighter", async (request) => {
  	const logger = Moralis.Cloud.getLogger();
  
	let FighterID = request.params.nftID;
  	let userID = request.params.userid;
  
  	const ActiveFighters = Moralis.Object.extend("ActiveFighters");
    const query = new Moralis.Query(ActiveFighters);
    query.equalTo("FighterID", FighterID);
    const results = await query.find();
  
    logger.info("Successfully retrieved " + results.length + " scores.");
  
  	const pointer = Moralis.User.createWithoutData(userID);
  
  	const activeFighter = new ActiveFighters();
  	activeFighter.set('FighterID', FighterID);
  	activeFighter.set("User", pointer);
  
  	await activeFighter.save();
}, {
  fields : ['nftID', 'userid'],
  requireUser: true
})

and this returns an error: Error: Validation failed. Please login to continue.

  • at resolveError (/moralis-server/lib/triggers.js:628:12)*
  • at /moralis-server/lib/triggers.js:657:21*

The user is already logged in to my app. Why does the validation fail?

Hi @ApesTogetherStrong

Please share your server subdomain

@Yomoo https://vn8enssw8uhp.grandmoralis.com:2053

The error message says
error: Failed running cloud function setActiveFighter for user undefined with:

This means that you are not logged in

@Yomoo yeah, but I’m logged in, that’s the issue. Do I have to pass something extra in the url?

The explanation would be that fetch("SERVERURL/server/functions/setActiveFighter?_ApplicationId=APPID&nftID=" + id + "&userid=" + user.id); would make a simple GET request without sending all current session information from current logged in user.

1 Like

@cryptokid that’s helpfull. Any ideas how to send the current sesstion information in react-moralis app?

Use the useMoralisCloudFunction() hook

1 Like

@Yomoo it worked using the useMoralisCloudFunction method, thanks!

2 Likes

You are welcome!

Happy BUIDLing :mage: