【发布时间】:2021-03-26 01:14:47
【问题描述】:
我正在尝试为 Odoo 生成 rest-api - 带有 Nestjs 和 odoo-xmlrpc 的 ERP。我可以连接到 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
【问题讨论】:
-
你混淆了使用回调和承诺的方法。没有显示使用承诺,这会使您的所有
await和async无用。回调中的return也不会返回到外部函数。您要么需要重构所有这些以对其进行承诺,要么使用标准回调方法
标签: node.js odoo nestjs xml-rpc