-
Hello! Please help me. Here is the code. import { prop, getModelForClass, Ref } from '@typegoose/typegoose';
import mongoose from 'mongoose';
export class Organization {
@prop({required: true})
name!: string;
@prop({
ref: () => User,
foreignField: 'organizationId',
localField: '_id'
})
public users: Ref<User>[] = [];
}
export class User {
@prop({required: true})
name!: string;
@prop({
ref: () => Organization,
required: true,
foreignField: '_id',
localField: 'organizationId',
justOne: true
})
private organization!: Ref<Organization>;
@prop({ required: true })
public organizationId!: string;
}
const OrganizationModel = getModelForClass(Organization, {
schemaOptions: {
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
});
const UserModel = getModelForClass(User, {
schemaOptions: {
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
});
async function main() {
try {
await mongoose.connect('mongodb://localhost:27017/', {dbName: 'migration-to-typegoose'});
const organization = await OrganizationModel.create({
name: 'Acme Inc.'
});
const users = await Promise.all([
UserModel.create({
name: 'Vasyl',
organizationId: organization._id.toString()
}),
UserModel.create({
name: 'Misha',
organizationId: organization._id.toString()
})
]);
const user = await UserModel.findOne().populate('organization');
// here is TS error that name does not exist on organization
console.log(user.organization.name)
// possible solutions
// 1) const user = await UserModel.findOne().populate<{organization: Organization}>('organization');
// 2) same as above but define interface
// 3) typecasting to any
// 5) if isDocument
process.exit(0);
} catch (err) {
console.error(err);
process.exit(1);
}
}
main(); There are 2 entities: organization and user. Organization has many users. I do populate on user (populating organization). After that I try acccessing .name on populated organization field. I get TS error. Basically my question is there any other way to access fields on populated field without interfaces, typecasts, isDocument? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
i think the error is missing
no, you need to use one of the following:
|
Beta Was this translation helpful? Give feedback.
-
Is this possible without casting, can typegoose automatically set the type when I do .populate for the field being populated? |
Beta Was this translation helpful? Give feedback.
-
When I use Ref<User> it should at least give the user type. Or what's even
advantage of Ref over using ObjectID?
…On Tue, Sep 19, 2023, 14:44 hasezoey ***@***.***> wrote:
no typegoose does not do this automatically, to do this automatically
typegoose would require to overwrite the mongoose populate type (which
typegoose avoids doing)
but mongoose provides *some* way to overwrite the path via a generic: .populate<{
field: PopulatedType }>(selector) (see mongoose documentation
<https://mongoosejs.com/docs/typescript/populate.html>)
also in addition populate modifies the document in-place, but typescript
does not support in-place type changes (to my knowledge) so to to actually
make the mongoose way useful, you would have to do doc = doc.populate()
(reassigning the value)
—
Reply to this email directly, view it on GitHub
<#813 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMSQ2BHO66RN7JX6CSS62S3X3GOTDANCNFSM6AAAAAAWYYQYYQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Ok, how do I filter that out please?
…On Tue, Sep 19, 2023, 18:22 hasezoey ***@***.***> wrote:
this is basically the question this discussion was originally about, see
comment #813 (reply in thread)
<#813 (reply in thread)>
TL;DR: Ref is a combination of the ref-type (objectid in most cases) and
the class itself, and need to be filtered out after population, because
population can fail
—
Reply to this email directly, view it on GitHub
<#813 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMSQ2BFLHZNNAH3RMZALXNDX3HIFZANCNFSM6AAAAAAWYYQYYQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
i dont know why i wrote this, but yes,
!
andNonNullable
do not help in this way, sorry about that.so to your original question, your only options are:
isDocument
via a early-return (viaif () return
orassertion
) or only operate in that if-block