building-modern-app-with-nextjs-and-mongodb's Issues
Dates
Great tutorial! Thanks so much for creating it.
Have one small question. I haven't been able to use findOne to get documents by date in the way you lay it out.
In daily.js your code is:
if(date){
doc = await req.db.collection('daily').findOne({date: new Date(date)})
} else {
doc = await req.db.collection('daily').findOne()
}
But because the dates in your data.json are formatted like "2020-01-22T08:00:00.000+00:00", the request does not work for me.
I ended up changing the date format to "2020-01-22T08:00:00.000Z" in the database and using the following code:
if(date){
doc = await req.db.collection('daily').findOne({date: new Date(date).toJSON()})
} else {
doc = await req.db.collection('daily').findOne()
}
But I am curious how it is supposed to work? And how you are supposed to request dates of your format?
Thanks!
Console is logging: "API resolved without sending a response for..."
Many thanks for putting this tutorial together @kukicado , this was very helpful to get me up and running with MongoDB and Next.js.
Your code is working for me, however each API call throws
API resolved without sending a response for /api/daily, this may result in a stalled requests.
to the console similar to issue 10439.
I was wondering if you are able to replicate this issue and if you had a fix?
maximum connections threshold
I've used this as a foundation for MongoDB connection, so far it works but I'm getting mails from MongoDB saying I've almost exhausted all my connections while i only have my website that access from Mongo and sometimes my local nodejs development server. What can be the cause?
my database.js
import nextConnect from 'next-connect';
const client = new MongoClient('mongodb+srv://username:[email protected]/?retryWrites=true&w=majority',
{
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function database(req, res, next) {
if (!client.isConnected()) await client.connect();
req.dbClient = client;
req.db = client.db('cercleSport');
return next();
}
const middleware = nextConnect();
middleware.use(database);
export default middleware;```
------------------------------------------------------------+
------------------------------------------------------------+
One of my api endpoints
pages/api/fetchallgames
//
```import nextConnect from 'next-connect';
import middleware from '@/middleware/database';
const handler = nextConnect();
handler.use(middleware);
handler.post(async (req, res) => {
// Convert the object back to a JSON string;
const league = req.body.league;
const division = req.body.division;
const fieldString = "${league}.${division}.games";
let projectionObject = {_id: 0};
projectionObject[fieldString] = 1;
let doc = await req.db.collection('24').findOne({}, { projection: projectionObject });
let games = doc[league][division].games;
return res.status(200).json(games);
});
export default handler;```
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.