OpenTelemetry 101

OpenTelemetry is an open-source project that provides a unified set of APIs, libraries, agents, and collector services to capture distributed traces, metrics, and logs from your applications and…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




MongoClient must be connected to perform this operation

After deploying my site, I kept getting this error and have spent the past couple days trying to figure it out.

The error appeared after I would go to a product’s show page and try to load the reviews.

Stackoverlow answered differed, and I could only check the answer with a deploy app as the problem was not arriving in localhost:3000.

It appears when the api call made to myapp.com/api/products/[id]/reviews, it was unable to GET the information.

This what my api call looked like

Everything appeared to be fine. Some answers to this problem suggested that the problem was actually in my ref when making a Model.schema with MongoDB. But, after reading around….ahhh finally the right answer.

The problem was that connect is async. The app wasn’t finished connecting to the db, and it was running other steps.

The answer to the problem lied in simply adding await before the line db.connect()

Also, here is what my connect function looks like.

And final code with await added

Now we make sure we connect to the db first, then we are able to search the products, and find the reviews for that product.

Add a comment

Related posts:

My journey with software development

If you told me that I know everything about software development then I probably would have laughed because no one is perfect in the world. There are three motivations works behind my starting the…

Essential Applications to Install on Ubuntu

Ubuntu is one of the most popular operating systems based on Linux, known for its ease of use and open-source nature. Once you have installed Ubuntu, the next step is to install the necessary…

My Dinner With Andre

There is a miracle in the cup of coffee. Stowed overnight, warmed in the morning and nary a cockroach to be found. It is something that Wallace’s friend Andre doesn’t see. Andre isn’t as far along as…