Unidentified properties in mongoose query

Currently, I am utilizing MongoDB and Mongoose along with TypeScript. I have encountered an issue with the following scenario:

Here is the model definition I have created:

export default conn.model<AdminInterface & Document>('Admin', adminSchema)

export interface AdminInterface {
  email: string,
  password: string,
  role: string,
  created: Date,
  author: {
    name: string,
    bio: string,
    githubUrl: string,
    stackoverflowLink: string,
    twitterLink: string,
    image: string,
    image_webp: string,
  },
}

No error is thrown at this stage.

My intention is to perform a basic query like this:

import { AdminInterface } from "../model/admin"
export function getAdmin(): Promise<AdminInterface | null> {
  return Admin.findOne({ role: 'admin' }, { password: 0 })
}

However, the following error is raised:

Type 'DocumentQuery<AdminInterface & Document, AdminInterface & Document, {}>' is missing the following properties from type 'AdminInterface': email, password, role, created, author

What mistake am I making? How can I specify the expected response format to the .findOne method?

Answer №1

Here is the solution I found:

To resolve the error, I added the .exec() method at the end:

import { AdminInterface } from "../model/admin"
export function getAdmin(): Promise<AdminInterface | null> {
  return Admin.findOne({ role: 'admin' }, { password: 0 }).exec()
}

Although I'm unsure if this is the best approach, I will proceed with using it this way.

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

Transforming a fluid webpage into a fixed page

Currently, I am undertaking a project that involves converting a dynamic website like Adobe Spark into a static HTML+CSS page, which will eventually be transformed into a PDF. The interactivity of the website relies heavily on Javascript modifying CSS+HTML ...

The DOM does not contain unscrolled rows in the DataTable's infinite scrolling feature

Every time I create a dataTable with infinite scrolling, I use the following code: $('#table1').dataTable({ 'aaData' : dataArr, 'aoColumns': columnArr, 'bScrollInfinite': true, 'bColumnCollapse& ...

Sorting by the primary date key and secondary alphanumeric in PHP can be accomplished by using a combination of

My 2D array has the following structure: [0] => Array( [date] => 23-01-2017 [name] => bbb [othertext] => text2 ) [1] => Array( [date] => 23-01-2017 [name] => aaa [othertext] => text3 ) [2] => Array( [ ...

Transforming a CSV file into JSON format using Gatsbyjs

I am currently exploring the possibilities of GatsbyJs and considering the utilization of the gatsby-transformer-csv plugin. You can find the documentation for this plugin here. I have got my hands on two CSV files exported from WordPress that I am eager ...

How to retrieve and remove entire nested array object by value in MongoDB using Mongoose

If a specific key in an object contains a certain string, I need to retrieve the entire nested array object. I am using mongoose with nodejs: Before deletion: { _id : 1234 fallBackData: { nestedKey: [ { arrayKey: "theValue" }, { arrayKe ...

Interactive loading of datalist choices using AJAX in the Firefox browser

I have recently decided to replace the Jquery UI autocomplete function on my website with HTML5 datalists that load dynamic options. After researching this topic extensively, I came across various answers on Stack Overflow, such as How do you refresh an HT ...

Enforcing TypeScript restrictions on method chaining during object creation

Let's consider this unique sample class: class Unique { public one(): Pick<this, "two" | "three"> { return this; } public two(): Pick<this, "three"> { return this; } public three(): string { ...

Guide to adding files to a WordPress post with Selenium in Python

I attempted to automate the creation of WordPress post content using Selenium Webdriver (Python), but I encountered an issue with uploading files in the post content. Despite searching for a solution, most methods involved send_keys which is not suitable f ...

Ways to identify and differentiate user clicks on various buttons

I have generated 3 different plan options from an array of objects retrieved from the backend. Depending on whether the plan is cheaper, the user's subscription, the corresponding button will display "downgrade", more expensive, the button will show ...

Show the time with AM/PM without displaying the year, month, or day

Currently, I am struggling to show the time as AM when my website loads using the format mm-dd-yyyy --:-- AM in a datetime-local input. The value attribute of datetime-local is causing some confusion for me. ...

Update the styling of each div element within a designated list

Looking for a way to assist my colorblind friend, I am attempting to write a script. This is the layout of the page he is on: <div class="message-pane-wrapper candy-has-subject"> <ul class="message-pane"> <li><div style=" ...

I'm having trouble getting the JADE tag to render in Express script. Can anyone help me

I am trying to include client-side script in my JADE template and so far I have: extends layout script. function collect_data() { var transitions = {}; $( ":checkbox:checked" ).each(function (index, element) { //// some code ...

Dealing with null exceptions in Angular 4: Best practices

Hi there, I am encountering an issue with binding my model data to HTML fields where when I try to edit the data it returns an error saying "cannot read value of null". How can I resolve this? Here is the HTML code snippet: <div class="form-group"> ...

What is the best way to add an array to my JSON object in Javascript?

I'm currently in the process of formatting an array into a JSON object for API submission. Struggling to find the right method to transform my array into the desired structure. This is what my array looks like: data: [ ["Lisa", "Heinz", "1993-04 ...

Add information to the database exclusively during the initial creation of a new MongoDB instance using Mongoose

Currently, I am working on a project involving NodeJs, Mongoose, and MongoDB. My challenge is to have a user in my web application that is automatically created once the database is initialized. This user creation should occur only once. Can anyone assist ...

Learn how to implement the JQuery ReplaceWith function with the resources provided by materials.io icon

I would like the favorite_border icon to switch to the favorite icon when clicked. As we are using material.io and both icons have the class material-icons, I am unsure about how to implement this using jQuery. What steps should I take to achieve this? (w ...

Looping through an array and adding its elements to a dictionary using JavaScript

I am struggling to figure out how to iterate over a dictionary with values in the form of arrays and then organize those values into a new dictionary. I am unsure of the steps needed to achieve this. Below is the data I need to iterate through: { t: [&quo ...

What could be causing my Svelte store's subscribe() method to trigger despite no change in value?

One of the Svelte stores I am using is called activeAccountIndexStore: export const activeAccountIndexStore: Writable<number | "native" | null> = writable(null); Initially, the value of the store is null, but it gets set to either a spec ...

basic handler in expressjs using the PUT method along with jQuery ajax

I am currently developing a web application utilizing a REST API for server communication. The backend is built with Node.js using Express.js. One issue I am running into is the inability to read the request body in PUT requests. Below is my client-side co ...

Gatsby causing issues with Material UI v5 server side rendering CSS arrangement

I shared my problem on this GitHub issue too: https://github.com/mui-org/material-ui/issues/25312 Currently, I'm working with the Gatsby example provided in Material UI v5: https://github.com/mui-org/material-ui/tree/next/examples/gatsby After imple ...