[SOLVED] Blocked by CORS policy

New here and Im just setting up my first cloud function to perform a simple save to the database but I seem to be getting the follow error

Access to XMLHttpRequest at ‘https://owexnxuzf2ev.usemoralis.com:2053/server/functions/createAvatar’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

	const logger = Moralis.Cloud.getLogger();
	logger.info("Request coming 6: " + JSON.stringify(request));

	// logger.info("User : " + JSON.stringify(user));

	request.user.set("username", "new_usernname");
	await user.save();

	return user;

As soon as I enable the save function I get this error

The server is brand new ganache and I have set up hardhat for my localchain

You could get that error when server is not working, when it gets a timeout or when you have something that blocks that connection like a vpn.

The server is running fine and I can even make calls via the js console

Can you look in browser network tab to see what it says there?

That is browser console. Browser network is somewhere near console, it is something different.

yes, this is the network tab

for me it worked if I paste directly in browser https://owexnxuzf2ev.usemoralis.com:2053/server/functions/createAvatar

Ok any idea what may be causing the cors policy error?

try to see if it works in the browser directly

Ok I just got one that passed and one that failed upon a single submission

Weird though

Yeah it works in the browser

ok, then try with http://127.0.0.1:3000’ instead of http://localhost:3000’

2022-04-29T19:24:24.096Z - ReferenceError: user is not defined

Moralis info logs
Error: {“message”:“user is not defined”,“code”:141}

1 single submission and it seems to be firing 2 times, perhaps this is the issue

1 fail 1 passs

that is expected one is OPTIONS for Request Method

click on the one where it says bad request

2 Likes

Seems there are 2 calls made one is a “post” and one is an “options”

{“code”:142,“error”:“Validation failed. Please login to continue.”}

Getting validation failed probably because of the “requireUser: true”

So the cors issues seems to have stop, not im getting set errors and user not defined

This fixed the CORS issue, thank you!

1 Like