When querying a document, the Mongoose array is missing

When querying for documents with an array, I'm encountering an issue where the returned documents do not include the array. Below is my code:

Query Code

(async () => {
        const data = await Lesson.find({signed: {$exists: true}});
        console.log(data[0].signed); # undefined
})();

Model Schema

const lessonSchema = new mon.Schema(
    {
        day: Number,
        startTime: Number,
        endTime: Number,
        description: {type: String, trim: true},
        signed: [mon.Schema.Types.ObjectId]
    }, 
    {
        collection: 'lessons'
    }
);
module.exports = mon.model("Lesson", lessonSchema);

Upon checking the database, I confirmed that the documents indeed have the required array field. However, when executing the query, it retrieves all other data except for the array, even though the array exists in all documents. It's worth noting that there are only two test documents in the database at this time.

Thank you for any insights or assistance provided. Additionally, I discovered that removing the 'signed' property from the schema resolves the issue. Does anyone know why?

Answer №1

The issue arose from using string IDs with a signed object type of `objectId`. Once I replaced the incorrect IDs with actual ObjectId values in the array, the problem resolved itself.

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

Converting arrays of objects in JavaScript made easy

Could someone please assist me in converting my array of objects into an object or JSON format? Here is a sample snippet: var data = [ {"code":"M","montant":"2000","title":"Masculine"}, {"code" ...

Using a vanilla JS object as a prop for a child component

I have created a custom Message class in my application to handle incoming messages, which is defined in message.js. Within message.js, I've implemented two classes: Message and EventEmit. The render function in my Message class requires passing an E ...

Looking to refine the query in order to retrieve a specific object in MongoDB

I have a document and I am looking to extract the "employeedata" that only contains array objects (family, academic etc.) with a status of "current" { "_id" : ObjectId("5a1fe7ed1e9fdd17285ac13f"), "createdby" : "admin", "details" : [ ...

Navigating the website with curtain.js and anchor tags

Currently, I am working on a website located at www.TheOneCraft.co.uk. I have incorporated the curtain.js jQuery plugin to create animated slide/pages as users scroll down. However, I have been unsuccessful in making the navigation bar follow this animati ...

javascript issues with ajax functionality

I am currently dealing with some JavaScript code that looks like this: alert(""); var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code fo ...

Difficulty validating RSA signature on the server end originating from client-side Javascript

Utilizing the forge library on the client side for creating signatures looks like this: //Client Side var md = forge.md.sha256.create(); md.update(encryptedVote, 'utf8'); var pss = forge.pss.create({ md: forge ...

What could be the reason for the failure of @babel/preset-react automatic transformation with Webpack?

The setup involves a Yarn monorepo with a babel.config.js that specifies the environment. React version being used is 18.1.x. I have a similar configuration that works perfectly fine in another setup, but it's failing here. I've been painstaking ...

Start the Vue component only after ensuring that the element has been fully loaded

I have created a small vue.js component in JavaScript. Is there an elegant way to instantiate the vue component only when the element is loaded? The problem I am facing is that I'm receiving warnings in other HTML files where the element does not ex ...

Is express-jwt assigning the user object to req.user._doc, as opposed to just req.user?

Previously, I have utilized the npm package express-jwt for effortless JWT signing and decoding. Typically (and as per the documentation), it decodes the token in a request, extracts the user object payload, and assigns it to req.user. However, this time a ...

How to iterate through an array of objects in Javascript and extract an array of strings

I am dealing with an array of objects that looks like this: [{"key":"aaa","value":true},{"key":"bbb","value":false},{"key":"ccc","value":true}] My goal is to iterate through it and extract an array containing only the keys: ["aaa", "bbb", "ccc"] I am u ...

Tips for customizing the `src/app/layout.tsx` file in Next.js 13

I am looking to customize the layout for my /admin route and its child routes (including /admin/*). How can I modify the main layout only for the /admin/* routes? For example, I want the / and /profile routes to use the layout defined in src/app/layout.ts ...

How can we effectively manage error responses and retry a failed call in NodeJS without getting tangled in callback hell?

I am in search of an effective approach to handle the given situation. I am curious if employing promises would be a helpful solution? Situation Overview: When a call retrieves a callback, and this callback receives an error object as a parameter. My obj ...

A distinctive noise is heard when hovering over multiple instances of a div

I'm trying to implement a feature where a unique sound plays when hovering over a specific div element with a particular class (.trigger). However, I am encountering an issue where multiple instances of this div class result in the same sound being pl ...

Creating a Checkbox Tree with JSON Data in MVC: A Step-by-Step Guide

I am working with JSON data that includes a parent-child relationship. My goal is to create a tree structure from this data. Within the JSON return value, we are sending two objects: DomainUserViews and ModuleUserViews. However, the current output displa ...

Managing actions with IconMenu and ListItem in React using MaterialUi

Currently, I am delving into the world of React and attempting to create a simple TODO list using Material-UI. However, I have encountered an issue with handling IconMenu menu actions within a listItem element. I am struggling with triggering the deleteI ...

Can we display an express view in response to a WebSocket event?

My goal is to display an express view within a websocket event as shown below: socket.on('name', function () { //prompt the client to render a specific express view }); ...

Unable to modify the appearance of a Bootstrap multi-select component

After creating a multi-select feature using Bootstrap styling, I ran into an issue where trying to hide it with style="display:none" did not work as expected. Can someone shed light on why this is happening and provide a solution? <link rel="styles ...

"Exploring the creation of multidimensional arrays in Arduino - what steps should I

Is there a way to create a multidimensional array in Arduino? I want something like this. C++ var arr = { name: "John", age: "51", children: [ "Sara", "Daniel" ] }; or maybe like this. JSON ...

What is the best method to initialize a JavaScript function only once on a website that uses AJAX

Currently, I am facing an issue with a javascript function that needs to be contained within the content element rather than in the header. This is due to a dynamic ajax reload process which only refreshes the main content area and not the header section. ...

The efficiency of the Angular app in production is hindered by a file that takes a whopping 5 seconds to load

After successfully compiling my project for production using the command ng build --prod, I made sure to implement certain production settings in angular.json: "production": { "fileReplacements": [ { "replace": "src/environments/e ...