Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating a subdocument with Model.updateOne complains with "missing following properties of LeanDocument: ownerDocument, parent " #10597

Closed
vkarpov15 opened this issue Aug 24, 2021 Discussed in #10595 · 4 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@vkarpov15
Copy link
Collaborator

Discussed in #10595

Originally posted by Duchynko August 24, 2021
Mongoose version: 5.13.7

After an update from 5.11.17 to 5.13.7, I'm seeing this error when trying to update a list of subdocuments.

Type 'IGameEvent[]' is not assignable to type 'LeanDocument<IGameEventDocument>[]'.
  Type 'IGameEvent' is missing the following properties from type 'LeanDocument<IGameEventDocument>': ownerDocument, parent
// gameRecord: IGameDocument
gameRecord.update({
    status: GameStatus[fixture.status.short],
    awayTeamScore: goals.away,
    homeTeamScore: goals.home,
    venue: fixture.venue.name,
    date: new Date(fixture.date),
    // this is complaining. mapFixtureEvents is returning IGameEvent[] and events is expecting LeanDocument<IGameEventDocument>
    events: FootballApiMappings.mapFixtureEvents(events),
});

How can I update a subdocument with a plain object?

// TypeScript doesn't complain here
gameRecord.events.push(FootballApiMappings.mapFixtureEvents(events));

Thanks :)

@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Aug 24, 2021
@vkarpov15 vkarpov15 added this to the 5.13.9 milestone Aug 24, 2021
@vkarpov15 vkarpov15 modified the milestones: 5.13.9, 6.0.6 Sep 2, 2021
@vkarpov15
Copy link
Collaborator Author

Try upgrading to v5.13.9, it looks like this may have been fixed with #10647 . I can confirm the below script fails to compile with v5.13.7 but compiles successfully with v5.13.9.

import { Document, connect, model, Schema } from 'mongoose';
  
run().catch(err => console.log(err));

async function run() {
  await connect('mongodb://localhost:27017/test');
  
  interface IGameEventDocument extends Document {
    description: string;
  }
  interface IGameDocument extends Document {
    name: string;
    events: IGameEventDocument[]
  }
  const schema = new Schema<IGameDocument>({ name: String, events: [{ description: String }] });

  const GameModel = model<IGameDocument>('Game', schema);

  const doc = await GameModel.findOne().orFail();
  await doc.update({ events: [{ description: 'test' }] });
}

@vkarpov15 vkarpov15 removed this from the 6.0.6 milestone Sep 7, 2021
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed typescript Types or Types-test related issue / Pull Request labels Sep 7, 2021
@imduchy
Copy link
Contributor

imduchy commented Sep 12, 2021

@vkarpov15 In my case, IGameEventDocument extends Subdocument not Document.

export type IGameEventDocument = IGameEvent & Types.Subdocument;

events field in update methods now expects IGameEventDocument instead of LeanDocument<IGameEventDocument>.

const gameRecord = await Game.findByIdAndUpdate(upcomingGame._id, {
    status: GameStatus[fixture.status.short],
    awayTeamScore: goals.away,
    homeTeamScore: goals.home,
    venue: fixture.venue.name,
    date: new Date(fixture.date),
    // Type 'IGameEvent[]' is not assignable to type 'IGameEventDocument[]'.
    //  Type 'IGameEvent' is not assignable to type 'IGameEventDocument'.
    //   Type 'IGameEvent' is missing the following properties from type 'Subdocument': $isSingleNested, ownerDocument, parent, $parent, and 51 more.
    events: FootballApiMappings.mapFixtureEvents(events), // <- Error here
});

@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Sep 15, 2021
@vkarpov15 vkarpov15 added this to the 6.0.8 milestone Sep 15, 2021
vkarpov15 added a commit that referenced this issue Sep 22, 2021
@vkarpov15
Copy link
Collaborator Author

@Duchynko Fix will be in v6.0.8. Are you able to upgrade to v6 or is there something that's blocking you from upgrading?

@imduchy
Copy link
Contributor

imduchy commented Sep 25, 2021

Thanks a lot @vkarpov15. I'll update to 6.0.8 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

2 participants