You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Mongoose version
7.0.1
Node.js version
18.12.1
MongoDB server version
5.0.15
Typescript version (if applicable)
5.0.2
Description
When using chaining .select() onto a findOne query, TypeScript doesn't recognise that only the specified fields will be present on the retrieved document, and subsequently doesn't complain if one tries to access a field that was omitted in the .select(), leaving us to rely on type casting or other measures which would otherwise be unnecessary.
Steps to Reproduce
import mongoose from 'mongoose'
interface UserI{
email: string;
passwordHash: string;
name: string;
username: string;
profileImageURL: string;
}
const userSchema = new mongoose.Schema<UserI>({
email: { type: String, required: true, unique: true, index: true },
passwordHash: { type: String, required: true },
name: { type: String, required: true, index: true },
username: { type: String, required: true, unique: true, index: true },
profileImageURL: { type: String, required: true },
})
const User = mongoose.model<UserI>("User", userSchema);
const getUserByID = async function (id: string) {
const account = await User.findById(id).select("username");
if (!account) return;
// TypeScript thinks the all user properties are present, within the retrieved document, and doesn't complain on the line below
account.profileImageURL
return account;
};
Expected Behavior
At the moment I'm using the following as a work-around - I'll leave it here just in case it's helpful:
This requires desired fields to be submitted in an array. I'm not sure how you'd go about this if the fields were submitted using a string in which fields are space separated, as is the case with mongoose's select.
The text was updated successfully, but these errors were encountered:
Prerequisites
Mongoose version
7.0.1
Node.js version
18.12.1
MongoDB server version
5.0.15
Typescript version (if applicable)
5.0.2
Description
When using chaining .select() onto a findOne query, TypeScript doesn't recognise that only the specified fields will be present on the retrieved document, and subsequently doesn't complain if one tries to access a field that was omitted in the .select(), leaving us to rely on type casting or other measures which would otherwise be unnecessary.
Steps to Reproduce
Expected Behavior
At the moment I'm using the following as a work-around - I'll leave it here just in case it's helpful:
This requires desired fields to be submitted in an array. I'm not sure how you'd go about this if the fields were submitted using a string in which fields are space separated, as is the case with mongoose's select.
The text was updated successfully, but these errors were encountered: