I have two different cloud functions in my Moralis server that A) GET data from Moralis DB and B) Update Moralis DB.
Now my next.js API Route has a GET and PATCH route but Moralis cloud function only accepts GET requests. So for my PATCH req Iโm using it to differentiate the API calls from the frontend but itโs still just a get request to Moralis DB cloud function to update DB.
Can cloud functions accept anything other than GET or am I doing something wrong here?
const handler: NextApiHandler = async (req: NextApiRequest, res: NextApiResponse) => {
await Moralis.start({
serverUrl,
masterKey,
appId,
});
const { id } = req.query;
const allowedMethods = ['GET', 'PATCH'];
if (!allowedMethods.includes(req.method!)) {
return res.status(405).send({ message: 'Method not allowed.' });
}
// Get Data
if (req.method === 'GET') {
try {
const response = await axios.get(
`${process.env.NEXT_PUBLIC_MORALIS_SERVER_URL}/functions/getItem?_ApplicationId=${appId}&id=${id}`
);
const data = response.data;
return res.status(200).json(data);
} catch (err) {
console.error(err);
return res.status(500).send({ err: 'Error retrieving trade' });
}
}
// Update Data
if (req.method === 'PATCH') {
try {
// I had to use a get request here because cloud function won't accept anything else even when I add a payload
const response = await axios.get(
`${process.env.NEXT_PUBLIC_MORALIS_SERVER_URL}/functions/cancelItem?_ApplicationId=${appId}&id=${id}`
);
const data = response.data;
return res.status(200).json(data);
} catch (err) {
console.error(err);
return res.status(500).send({ err: 'Error retrieving item' });
}
}
};
export default handler;