I am trying to query MongoDB hosted on the atlas (free tier). I am using nextjs for building api. I tried to host it in vercel (formerly known as zeit). The code works fine in local. On deploying to the cloud (vercel), it doesn't works.
var MongoClient = require('mongodb').MongoClient;
// serverless function for proverbs of the day
export default async (req, res) => {
return new Promise((resolve) => {
let databaseString = process.env.DATABASE_URL;
MongoClient.connect(
databaseString,
{ useUnifiedTopology: true },
async function (err, client) {
if (err) {
console.error(err);
throw err;
}
var db = client.db('database_name');
var start = new Date();
start.setHours(0, 0, 0, 0);
var end = new Date();
end.setHours(23, 59, 59, 999);
let result;
try {
// query 1 : doesn't works
result = await db
.collection('proverbs')
.findOne({ createdAt: { $gt: start, $lt: end } });
// query 2 : works
// result = await db.collection('proverbs').findOne({'id': 11 })
} catch (err) {
console.error(err);
} finally {
client.close();
}
res.send(result);
return resolve();
},
);
});
};
i tried two query : query 1 and query 2
in local both query 1 and 2 works
in vercel cloud server query 1 doesn't works query 2 works
i want to make the query 1 work. where am I going wrong?
start,endandresultin production and compare with localhost.