Why Next.js and MongoDB (minus Mongoose) is giving back an empty array

I am currently learning how to use API routes in Next.js with sample data retrieved from MongoDB. My challenge lies in attempting to retrieve data from a single object, which is proving more difficult than expected as I am new to working with MongoDB.

After executing the following query, I am consistently faced with an empty array being returned:

import { connectToDatabase } from '../../../util/mongodb';

export default async (req, res) => {
  const { db } = await connectToDatabase();

  const movies = await db
    .collection("movies")
    .find({ "_id": "573a1395f29313caabce1f51" })
    .limit(20)
    .toArray();

  res.json(movies);
};

The query should correspond to an object structured like this:

{
"_id": "573a1395f29313caabce1f51",
"fullplot": "some text goes here"
}

My dilemma now is figuring out what I am overlooking. Shouldn't

.find({_id: "573a1395f29313caabce1f51"})
successfully return the specified information? Why does it only produce an empty array? Although I understand why an array is returned due to the .toArray() method, it shouldn't affect the outcome of the results.

It's worth mentioning that querying without any parameters functions correctly. There are no issues when running the following query:

import { connectToDatabase } from '../../util/mongodb';

export default async (req, res) => {
  const { db } = await connectToDatabase();

  const movies = await db
    .collection("movies")
    .find({})
    .sort({ metacritic: -1 })
    .limit(20)
    .toArray();

  res.json(movies);
};

Any insights or assistance provided would be greatly appreciated. Thank you in advance!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript Code to Remove an Element from an Array

I have a Javascript Filter Item Class and an array structured as follows: function FilterItem(filterId, filterType) { this.filterId = filterId; this.filterType = filterType; } var filterItemArray = []; To add Filter Items to this array, I use th ...

Troubleshooting multiple file upload errors in Asp.net Mvc using ajax

I am attempting to implement multiple file upload in MVC using jQuery ajax but I am encountering some issues. https://i.sstatic.net/34kXO.png Here is my HTML design and code: <div id="fileinputdiv"> <input type="file" name="mainfile" id="main ...

Get the String from a Formatted Message

Seeking guidance on extracting and exporting the string part from FormattedMessage, specifically for CSV usage. Below is the snippet of my FormattedMessage code (IntlMessages.js): import React from 'react'; import {FormattedMessage, injectIntl} ...

Establish a connection between MongoDB and the built-in API in Next.js

I've been working on integrating a MongoDB database with the Next.js built-in API by using the code snippet below, which I found online. /api/blogs/[slug].ts import type { NextApiRequest, NextApiResponse } from 'next' import { connectToData ...

Methods for transferring data to controller in Angular Modal service

After implementing the angular modal service discussed in this article: app.service('modalService', ['$modal', function ($modal) { var modalDefaults = { backdrop: true, keyboard: true, m ...

What is the best way to remove an object element by index within AngularJS?

One of my challenges involves dealing with objects, specifically $scope.formData = {} I am trying to figure out how to remove an element from the object using the index $index: $scope.formData.university[$index]; My attempt was: $scope.formData.univer ...

Sender receives a response from Socket.io

My goal is to have a socket respond only to the sender. Currently, I am working on having the user connect to the server using JavaScript when they visit any webpage. However, I am unsure whether the connection will be reset each time the user reloads th ...

What causes the discrepancy between the nodejs package on GitHub and the version downloaded through npm?

After committing to installing a nodejs package with npm install -g package_name, I noticed that some of the downloaded packages have different files compared to the same package on Github. Why is this discrepancy present? ...

Jquery Validation Plugin - Specifically for validating numeric inputs in specific situations

I am currently working on implementing validation using jQuery Validate for a numeric value, but only when a specific radio button is checked. If the radio button is not checked, the field should be required and allow alphanumeric values to be entered. How ...

The pages are constantly showing an error message that reads, "There is a problem with the SQL syntax in your code."

I am having trouble with my login page as it is displaying an error on my index.php file. Below is the code snippet from my index.php: <?php include('supsrwk_epenyenggaraan.php'); ?> <?php if (!function_exists("GetSQLValueString")) { f ...

What's the best way to format text as bold in a .ts file so that it appears as [innerText] in the HTML section?

When looking to emphasize specific text without using [innerHTML], what is the alternative method besides the usual line break changes in the interface? How can we make certain text bold? For instance: .ts file string = This is a STRING bold testing.&bso ...

Sustain the operation of the Express application on a Linux-based cloud server

Having recently ventured into node/express development after working with Django and Apache in the past, I've encountered an issue with keeping my express app running with MongoDB after logging out of the Linux server. Despite my research, there seems ...

What is the method for applying a Redux statement?

Experience with Redux toolkits: https://i.sstatic.net/cwu8U.png I've encountered an issue while working with Redux toolkits where I'm unable to access certain statements. I attempted the following code snippet, but it resulted in an error. c ...

Combining JavaScript objects with matching keys and values into an array

I'm currently working on merging JavaScript objects that share the same key. Specifically, I intend to use the "time" field as the key and include every "app" as a key with its corresponding "sum" value for each time entry. The other fields will not b ...

Aligning hyperlink with full-height image within a table cell (navigation buttons)

I am currently working on creating a traditional navigation bar that occupies 20% of the screen width and consists of 3 buttons. Each button is supposed to have an icon that is centered both vertically and horizontally. Additionally, I want the buttons to ...

When I refresh the page in Angular2, the router parameters do not get loaded again

When loading my application on routers without parameters, everything is normal. However, when trying to use a router with params, the application fails to load. For example: localhost:3000/usersid/:id The code for the router is as follows: const appRou ...

405 error: NGINX blocking POST method in Django DRF Vue.js application

I have encountered a strange issue while building my ecommerce site. Everything seems to be working fine, except for the forms. When attempting to post content, I keep receiving a 405 method get not allowed error. It's confusing as I am trying to use ...

Guide on successfully implementing "Sign in with Twitter" using NodeJS

Struggling to get the Sign In with Twitter feature to work, I keep encountering the following error Whoa there! The request token for this page is invalid. It may have already been used or expired due to its age. Please return to the site or applicati ...

Why is the view not reflecting the updates made to the model?

I recently started delving into Angular JS and have been following tutorials to learn it. One issue I'm facing is that my model doesn't seem to update the view. Can anyone help me figure out why this is happening? Here is the code snippet: < ...

Sorting values from field array in Mongodb to create a ranking

I have been struggling with sorting a MongoDB aggregate and I can't figure out what is going wrong. I tried looking for solutions on Stack Overflow, but none of them seem to work and I'm not sure why... My goal is to return a ranking of values f ...