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

Odoo 17中使用useService调用ORM时触发TypeError错误求助

问题分析与解决方案

错误根源

Cannot use 'in' operator to search for 'orm' in undefined 这个错误本质是:你的组件没有运行在Odoo Web的服务容器环境中,useService 无法找到对应的服务上下文(services 对象为undefined)。

Odoo的useService钩子依赖Web框架提供的服务容器,而如果直接使用Owl原生的mount方法挂载组件,组件不会被注入Odoo的服务环境,自然无法获取到orm这类核心服务。

修复步骤

1. 改用Odoo Web的挂载方式替换原生Owl挂载

找到路由对应的模板文件(比如templates/owl_app_page.xml),修改组件初始化逻辑,使用Odoo Web提供的mount函数:

<template id="owl_app_page" name="Owl App Page">
    <t t-call="website.layout">
        <div id="owl-app-root" class="container mt-5"/>
        <script type="text/javascript">
            odoo.define('odoo_owl_app.root_mount', function (require) {
                const { mount } = require('@web/core/mount');
                const { Root } = require('odoo_owl_app.root');

                // 用Odoo Web的mount替代Owl原生mount
                mount(Root, document.getElementById('owl-app-root'));
            });
        </script>
    </t>
</template>

2. 确认模块依赖Web组件

检查__manifest__.py文件,确保depends列表包含web:

'depends': ['web'],

3. 正确使用ORM异步获取数据

在Root组件的setup中,通过onWillStart钩子异步调用ORM加载数据(ORM调用为异步操作):

setup() {
    this.todos = useState([]);
    this.orm = useService("orm");
    this.model = 'odoo_owl_app.todo';

    // 保留原有的handleChangeState、handleEdit等方法

    // 异步加载初始数据
    onWillStart(async () => {
        // 调用ORM的searchRead方法获取待办事项
        const todoRecords = await this.orm.searchRead(
            this.model, 
            [], // 域条件,空表示获取所有记录
            ['id', 'title', 'is_completed'] // 需要读取的字段
        );
        // 更新状态数据
        this.todos.splice(0, this.todos.length, ...todoRecords);
    });
}

验证修复

  1. 重启Odoo服务
  2. 重新安装/升级odoo_owl_app模块
  3. 访问/owl-app路由,此时应能正常加载ORM数据,不再出现之前的错误

内容的提问来源于stack exchange,提问作者Amir Rismaani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 18:23:29