跳至内容
菜单
此问题已终结
1 回复
4579 查看

Hello everyone, I am developing a small Todo List app on Odoo 17 to learn Owl. Everything was progressing smoothly until I attempted to dynamically fetch and display data using orm with useService. I encountered the following error:


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


I have extensively tried to resolve the issue by consulting the Owl and Odoo documentation, as well as searching for solutions online, but without success. The code adheres to the documentation, and I am uncertain about the source of this error.


Steps to Reproduce:

1. clone this repo somewhere

2. start odoo with this repo in the addons-path, on a recent version (odoo 17)

3. install the odoo_owl_app addon

4. go to the /owl-app route to see the result.


Code:

The root.js file contains the following codes:


/** @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)
        };
    }
}



When this code  this.orm = useService("orm"); is added to the setup, the following error is issued:



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



Screenshot:

![Screenshot from 2023-12-01 18-58-50](https://github.com/odoo/owl/assets/42430858/49b653a0-67c0-4c8f-b254-604262788b9f)



Additional Information:

- Odoo Version: 17


Projects:

-  this repo


I appreciate any assistance in resolving this issue and gaining a better understanding of using Owl with Odoo.

形象
丢弃
编写者 最佳答案

The problem was that I hadn't started the service, so useService couldn't work.


To solve this problem, I used mountComponent instead of mount in the app .js file.


More information at https://www.odoo.com/documentation/master/developer/howtos/standalone_owl_application.html

形象
丢弃
相关帖文 回复 查看 活动
1
9月 23
5125
1
8月 23
411
1
6月 24
3296
1
12月 23
1685
1
10月 23
3957