返回带有节点的api rest



我正在尝试使用Nestjs和Odoo-xmlrpc为Odoo-ERP生成rest api。我可以连接到odoo,但我无法响应odoo对我服务的响应。

这是响应值或错误的odoo服务:

import { Injectable, OnModuleInit } from '@nestjs/common';
@Injectable()
export class OdooService  {
private Odoo = require('odoo-xmlrpc');
public odoo;
constructor () {
this.odoo = new this.Odoo({
url: 'https:/www.xxxxxxxxx.xxx',
db: 'xxxx',
username: 'username',
password: 'password'
});
}
execute(model: string, funtion: string , params: any[], callback) {
const odoo = new this.Odoo({
url: 'https:/www.xxxxxxxxx.xxx',
db: 'xxxx',
username: 'username',
password: 'password'
});
odoo.connect(async function (error: any): Promise<any> {
if (error) {
console.log(error); 
return [{}];
}
odoo.execute_kw(model, funtion, params, function (error: any, values: []) {
if (error) { 
console.log('error :>> ', error);
return [{}];
}
callback(values);
})
})
}
}

这是使用odooServive 的服务

import { Injectable } from '@nestjs/common';
import { OdooService } from '../odoo/odoo.service';
import { CreateCountryDto } from './dto/create-country.dto';
import { UpdateCountryDto } from './dto/update-country.dto';
@Injectable()
export class CountriesService {
constructor(
private odooService: OdooService
) {}
async findAll() {
return await this.odooService.execute('res.country', 'search_read', [[[]], { fields: ['name'] }], async function (response: []) {
//  console.log(response);
return  await response;
});
}
}

odoo文档:https://www.odoo.com/documentation/14.0/webservices/odoo.html库文档:https://www.npmjs.com/package/odoo-xmlrpc

将@charlietfl的注释细化为一个答案,您可以使用其中一个代码,可以使用回调,也可以使用异步等待

无承诺,无回调

findAll() {
return this.odooService.execute('res.country', 'search_read', [[[]], { fields: ['name'] }]);
}

使用回调,无需Promise

findAll() {
this.odooService.execute('res.country', 'search_read', [[[]], { fields: ['name'] }], function (response: []) {
return response;
});
}

使用Promise

async findAll() {
const response = await this.odooService.execute('res.country', 'search_read', [[[]], { fields: ['name'] }]);
return response;  
}

最新更新