You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Strapi全局模型来源及使用疑问:全局Item与strapi.models.item的区别

Answers to Your Strapi Model & Service Questions

Hey there! Let's walk through each of your questions step by step to get you sorted out.

1. When is the global Item object created?

That global Item you're seeing in /api/item/services/Item.js is automatically injected by Strapi during its startup process. Here's the breakdown:

  • When Strapi boots up, it first scans all your content types (like the "Item" you created via the admin panel) and parses their schema definitions.
  • Next, it creates model instances for each content type, based on the schema and any customizations you've made.
  • Before loading your service files (like Item.js), Strapi injects the corresponding model instance as a global variable directly into the service's execution context. That's why you can use Item without importing it—it's already available in that specific service file's scope.

2. What's the difference between the global Item and strapi.models.item?

At their core, they reference the same model instance—but their accessibility and scope differ:

  • Global Item: This is scoped only to the /api/item/services/Item.js file. You can't use this variable in other services, controllers, or middleware files—it's a convenience injection just for the service tied to the "Item" content type.
  • strapi.models.item: This is a globally accessible reference to the same model, available anywhere in your Strapi codebase where you can access the strapi object (controllers, middleware, other services, etc.).

Functionally, both let you perform the same model operations (like create, find, update), but the global Item is just a shortcut for the service file it's injected into.

3. Best way to use the Item model when adding GraphQL support via middleware?

First off, avoid relying on the global Item variable in middleware—it won't be available there (since that injection only happens for service files). Your best options are:

Option 1: Use the service (strapi.services.item)

If you've added custom business logic to /api/item/services/Item.js (like validation, data transformation, or complex queries), this is the preferred approach. Strapi's best practice is to encapsulate all business logic in services, so calling strapi.services.item lets you reuse that logic directly in your middleware.

Option 2: Use the model directly (strapi.models.item)

If you only need basic CRUD operations and haven't added custom service logic, you can use strapi.models.item to access the model's core methods. However, even if you don't have custom logic yet, using the service is still a good habit—it keeps your code consistent and makes it easier to add logic later without refactoring.

When adding the model to your middleware's context, go with strapi.models.item if you need direct access to the model layer, or strapi.services.item if you want to leverage your service's business logic. Either way, accessing them via the global strapi object is the safe, reliable method in middleware.

内容的提问来源于stack exchange,提问作者Dmitrii Dushkin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:06:23