I have created a simple cloud function on moralis to fetch and store currency rates for a given currency code:
const axios = require("axios");
const { allowed_currencies } = require("../functions/constans/other");
const create_api_message = require("../functions/methods/other/create_api_message");
const { openexchangerates: { API_key } } = require("../functions/config");
Moralis.Cloud.define("get_rates_for_currency", async ({ params: { currency }}) => {
const five_days_miliseconds = 4.32e+8
try {
// For better caching
const rates = Moralis.Object.extend("currency_rates");
const currency_rates = new Moralis.Query(rates)
Moralis.Cloud.getLogger().info(currency_rates)
// Check if rates exist and they are max 5 days old
if (
currency_rates &&
currency_rates[currency] &&
(Date.now() - (currency_rates[currency].timestamp * 1000)) < five_days_miliseconds
) return currency_rates[currency] // Send cached rates
const { data } = await axios.get(`https://openexchangerates.org/api/latest.json?app_id=${
API_key
}&base=${
currency
}&symbols=${
Object.values(allowed_currencies).join(",")
}`)
const rates_info = { rates: data.rates, timestamp: data.timestamp, base: data.base }
const updated_rates = new rates();
updated_rates.set(currency, rates_info)
updated_rates.save()
return rates_info
} catch ({ message}) {
return create_api_message("danger", message)
}
});
And now am trying to call it both through Postman and the Function test calls in Moralis dashboard.
However I keep getting:
<!DOCTYPE html>
<head>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<base href="/"/>
<script>
PARSE_DASHBOARD_PATH = "/";
</script>
</head>
<html>
<title>Moralis Dashboard</title>
<body>
<div id="login_mount"></div>
<script id="csrf" type="application/json">"RWlPRCPU-7VloXr0iW-l25hVzCCE3ITKEdhQ"</script>
<script src="/bundles/login.bundle.js"></script>
</body>
</html>
The function should reside here: https://myAccount.usemoralis.com:2083/server/functions/get_rates_for_currency?_ApplicationId=myAppID¤cy=USD
Am I doing something wrong?