'Hide all hidden mongoose methods and properties
I use nuxt 3. I have this code on my serverside.
findAuthorByEmail(email: string, showPassword = false) {
return Author.findOne({ email })
.select(showPassword ? "" : "-password_hash")
.lean();
},
Inside my /server/api/login.post.ts i return it here:
return {
token,
author,
};
Nuxt has the feature to provide the types for my front end. What should my query look like so that my response only provides the properties which are available? If I try to use for example $isDefault then it does not work of course.
Here is also my mongoose model:
import { Document } from "mongoose";
import { AuthorI } from "~~/types";
import { conn } from "../configuration/connection";
import { authorConfig } from "../configuration/author";
import mongoose from "mongoose";
const schema = new mongoose.Schema({
email: {
required: true,
type: String,
trim: true,
lowercase: true,
unique: true,
},
password_hash: {
type: String,
required: true,
trim: true,
minlength: authorConfig.password_min_length,
},
name: {
maxlength: authorConfig.name_length,
trim: true,
type: String,
},
family_name: {
type: String,
maxlength: authorConfig.family_name_length,
trim: true,
},
description: {
type: String,
maxlength: authorConfig.description_length,
trim: true,
},
props: {},
username: {
type: String,
trim: true,
unique: true,
required: true,
maxlength: authorConfig.username_length,
},
created: {
type: Date,
required: true,
default: Date.now,
immutable: true,
},
enabled: {
type: Boolean,
default: true,
},
});
export const Author = conn.model<AuthorModel>("Author", schema);
interface AuthorModel extends AuthorI, Document {}
Solution 1:[1]
use .lean() method. check official doc mongoose lean
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Himanshu |

