0

Let's say, I have two models User and Post, and they are associated as one to many relationship:

const User = sequelize.define('user', {
    ...
});

const Post = sequelize.define('post', {
    ...
});

User.hasMany(Post);

Then if I get some posts from a user using getPosts method,

const user = await User.findOne({...});
const posts = await user.getPosts();
                         ^^^^^^^^

Typescript makes an error:

error TS2339: Property 'getPosts' does not exist on type '{}'.

What should I do for it? Any suggestions will be appreciated.

1 Answer 1

1

From what I read, the TypeScript typings for Sequelize are incomplete. You can specify the attributes of your objects when you call sequelize.define as shown here, but this won't declare a getPosts method AFAIK. The sequelize-typescript package appears to offer an alternative way to get related objects, though I haven't tried any of this myself.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't want to use sequelize-typescript :( but I should use it unavoidably.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.