在 Odoo 17 中使用 orm 和 useService 时出现错误

问题描述 投票:0回答:1

我正在 Odoo 17 中使用 Owl 开发一个待办事项列表应用程序。一切都很顺利,直到我尝试使用 orm 和 useService 动态获取和显示数据。我遇到了以下错误:

Uncaught (in promise) TypeError: Cannot use 'in' operator to search for 'orm' in undefined

重现步骤:

  1. 克隆这个仓库某处
  2. 在最新版本的插件路径中使用此存储库启动 odoo (
    odoo 17
    )
  3. 安装
    odoo_owl_app
    插件
  4. 前往
    /owl-app
    路线查看结果。

代码:

root.js
文件包含以下代码:

/** @odoo-module */

import { Component, useState, useSubEnv, onWillStart } from "@odoo/owl";
import { useService } from "@web/core/utils/hooks";


import { WebsiteLayout } from "./layouts/website/layout"
import { TodoForm } from "./components/todo_form/todo_form";
import { Todos } from "./components/todos/todos";

export class Root extends Component {
    static template = "odoo_owl_app.root";
    static components = { WebsiteLayout, TodoForm, Todos };
    static props = {};

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

        this.handleChangeState = (event) => {
            const id = event.target.value;
            const index = this.todos.findIndex(todo => todo.id === parseInt(id))
            this.todos[index].is_completed = !this.todos[index].is_completed
        };

        this.handleEdit = (id, title) => {
            const index = this.todos.findIndex(todo => todo.id === parseInt(id))
            this.todos[index].title = title
        };

        this.handleRemove = (id) => {
            console.log('remove', id)
            const index = this.todos.findIndex(todo => todo.id === parseInt(id))
            this.todos.splice(index, 1);
        };

        this.onAdd = (title) => {
            const todo = {
                userId: 1,
                id: new Date().getTime(),
                title,
                is_completed: false
            }
            this.todos.push(todo)
        };
    }
}

将此代码

this.orm = useService("orm");
添加到设置中时,会发出以下错误:

Uncaught (in promise) TypeError: Cannot use 'in' operator to search for 'orm' in undefined

附加信息:

  • Odoo 版本:17

项目:

感谢您在解决此问题以及更好地了解 Owl 和 Odoo 的使用方面提供的任何帮助。

我查阅了 Owl 和 Odoo 文档,并在网上搜索了解决方案,但未能解决问题。代码遵循文档,我希望动态数据检索能够正常工作。

javascript orm odoo
1个回答
0
投票

错误主要是由于安装问题造成的,因此请确保您在 javascript 文件中使用了

mountComponent
而不是
mount

© www.soinside.com 2019 - 2024. All rights reserved.