I have database queries setup in my Next.js API routes and it works fine on local dev environment but it does not return any results when uploaded to Vercel.
Should these queries be in cloud functions instead?
Localhost returns results for a specific endpoint. Production URL returns null. Both are calling the same database and same data so both should return the same.
// @route POST api/v1/asset/:address/:tokenId/listing-status
// @desc Get listing status (trade and sale) for token Id
// @access Public
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
import * as Sentry from '@sentry/nextjs';
import Moralis from 'moralis-v1/node';
import { ALLOWED_ADDRESSES } from '@/lib/allowed-addresses/allowedAddress';
const serverUrl = process.env.NEXT_PUBLIC_MORALIS_SERVER_URL;
const appId = process.env.NEXT_PUBLIC_MORALIS_APP_ID;
const masterKey = process.env.masterKey;
const handler: NextApiHandler = async (req: NextApiRequest, res: NextApiResponse) => {
const allowedMethods = ['GET'];
if (!allowedMethods.includes(req.method!)) {
return res.status(405).send({ message: 'Method not allowed.' });
}
if (req.method === 'GET') {
await Moralis.start({
serverUrl,
masterKey,
appId,
});
const { tokenId } = req.query as {
tokenId: string;
};
if (!ALLOWED_ADDRESSES.includes(token_address)) {
return res.status(405).send({ message: 'Address is not valid' });
}
try {
// const currentUser = await Moralis.User.current();
const PolygonItem = Moralis.Object.extend('PolygonItems');
const query = new Moralis.Query(PolygonItem);
query.select(
'token_address',
'token_id',
'owner_of',
'for_trade',
'for_sale',
'listing_price',
'duration',
'expiry',
'sale_token',
'order'
);
query.equalTo('token_address', token_address);
query.equalTo('token_id', tokenId);
console.log(`PolygonItem LISTING STATUS | Query: ${JSON.stringify(query)}`);
const item = await query.first({ useMasterKey: true });
if (!item) {
return res.status(200).json(null);
}
console.log(`PolygonItem LISTING STATUS | Results: ${JSON.stringify(item)}`);
return res.status(200).json(item);
} catch (err) {
Sentry.captureException(err);
console.error(err);
return res.status(500).json({ message: `Error updating item trade status. ${err}` });
}
} else {
res.send({
message: "Something's not right. Check your API call. Note, this route only accepts post requests!",
});
}
};
export default handler;