我正在 Odoo 17 中使用 Owl 开发一个待办事项列表应用程序。一切都很顺利,直到我尝试使用 orm 和 useService 动态获取和显示数据。我遇到了以下错误:
Uncaught (in promise) TypeError: Cannot use 'in' operator to search for 'orm' in undefined
重现步骤:
odoo 17
)odoo_owl_app
插件/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
附加信息:
项目:
感谢您在解决此问题以及更好地了解 Owl 和 Odoo 的使用方面提供的任何帮助。
我查阅了 Owl 和 Odoo 文档,并在网上搜索了解决方案,但未能解决问题。代码遵循文档,我希望动态数据检索能够正常工作。
错误主要是由于安装问题造成的,因此请确保您在 javascript 文件中使用了
mountComponent
而不是 mount
。