0

I am a beginner in angular 2 and I have this script that I need to integrate in an angular2 project to fetch some data from odoo :

const Odoo = require('odoo-connect');

const odoo = new Odoo({
    host: 'demo',
    port: 80
});

var project_list = [];

odoo.connect({
        database: 'database',
        username: 'admin',
        password: 'admin'
    })
    .then(client => {
        return client.call('project.project', 'search_read', [], {});
    })
    .then(projects => {
        for (var i = 0; i < projects.length; i++) {
            var project = projects[i];
            project_list.push(project['name'])
        }
        console.log(project_list);
    });

I just want to display the data in one simple page just to test it , before including it in the main project.I tried to build a trial project with Angular-CLI and for app.component.ts :

import {Component } from '@angular/core';
import { OnInit } from '@angular/core';
import { Input } from '@angular/core';
import * as connect  from 'odoo-connect';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit{

  private host:string;
  private port:number;

  @Input() project_list;

  constructor(){
    this.host='demo';
    this.port=80;
  }
  ngOnInit(){
    let  project_list : Array<any> = [];
    const odoo = new connect({
      host: 'demo9',
      port: 80
    });
    odoo.connect({
        database: 'database',
        username: 'admin',
        password: 'admin'
      })
        .then((client:any) => {
          return client.call('project.project', 'search_read', [], {});
        })
        .then((projects:any) => {
          for (let i = 0; i < projects.length; i++) {
            let project = projects[i];
            project_list.push(project['name'])
          }
          console.log(project_list);
        })
  }}

I get this in my console : webpack: Compiled successfully , but in the browserconsole I get this : Uncaught TypeError: Cannot read property 'prototype' of undefined

2

2 Answers 2

0

const Odoo = require('odoo-connect'); const odoo = new Odoo({ host: 'demo', port: 80 });

var project_list = [];

odoo.connect({ database: 'DB', username: 'admin', password: 'admin' }) .then(client => { return client.call('project.project', 'search_read', [], {}); }) .then(projects => { for (var i = 0; i < projects.length; i++) { var project = projects[i]; project_list.push(project['name']) } console.log(project_list); });

Sign up to request clarification or add additional context in comments.

Comments

0

After going around on this a few times, I couldn't get the odoo-connect npm package to install straightforwardly in Angular 2. I tried everything I know how to do and got close, but ultimately was defeated by a JSONStream error, meaning I probably need a different kind of loader, meaning I'd probably have to modify the webpack configuration...ugh. Only so much time in a morning.

However, this NPM package worked exactly as advertised:

https://www.npmjs.com/package/angular2-odoo-jsonrpc

With one exception: in the constructor where the injection is done:

constructor ( oodo : OodoRpcConnect ) 

make sure to include "private"

constructor ( private oodo : OodoRpcConnect )

to make the red squiggle go away.

Otherwise I followed the instructions to install/use exactly as they indicate and it worked fine.

So...

  • ng new MyProject
  • cd MyProject
  • npm install --save-dev angular2-odoo-jsonrpc
  • npm serve
  • (Make code changes as indicated in the package instructions)
  • Profit!

I'm using the latest and greatest Angular-CLI (upgraded today). If you think your CLI may be out of date, update it:

https://www.npmjs.com/package/angular-cli#updating-angular-cli

8 Comments

I keep getting : cannot find name ' require' in odoo.service.ts
If you're using the CLI, add the odoo-connect script to the "scripts" section of the angular-cli.json file (see my article, "Using External Scripts in Angular 2 (CLI)" at www.tcoz.com/#/errata). Compile, if all goes well, you should be able to remove the "require" statement (I think, since Odoo should now be global).
You may also want to look at this: npmjs.com/package/angular2-odoo-jsonrpc
thank you @Tim , I already tried with that package , but some of the functions are not available for odoo >= 9.0
I added the script into the section " scripts" in angular-cli.json file , but I still get the same error
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.