service.list is not a function Error
If you get the following error when using Query to retrieve records of your custom data model:
Why this Error Occurred#
To retrieve records of your data model, Query uses the listX method of your module's service.
For example, if you have a Blog Module and you are trying to retrieve posts, Query will try to call the listPost method of the Blog Module's service. If it doesn't find the method, it will throw the above error.
How to Fix it#
Option 1: Extend Service Factory#
To resolve this error, make sure that your module's service has a listX method that returns the records of your data model.
This method is generated for you if your service extends MedusaService:
In the above example, the listPost method is generated for you because the BlogModuleService extends MedusaService.
Option 2: Implement the Method#
If your module is returning data from a third-party service, or you have some custom mechanism to define and manage your data models, then you need to implement the list or just list method in your service.
For example:
1type BlogModuleOptions = {2 apiKey: string3}4 5export default class BlogModuleService {6 private client7 8 constructor({}, options: BlogModuleOptions) {9 this.client = new Client(options)10 }11 12 async list(13 filter: {14 id: string | string[]15 }16 ) {17 return this.client.getPosts(filter)18 /**19 * Example of returned data:20 * 21 * [22 * {23 * "id": "post_123",24 * "product_id": "prod_321"25 * },26 * {27 * "id": "post_456",28 * "product_id": "prod_654"29 * }30 * ]31 */32 }33}
Additional Resources#
- Query
- Service Factory documentation
- Service Factory reference
- Example: Read-Only Module Link for Virtual Data Models