I am using node-odoo. which is simple RPC call to Odoo System.
Component build using prototype object.
https://github.com/isotopo/node-odoo/blob/master/lib/index.js
I am converting callback function to Promise but it a throwing error. Its seems like context missing.
const Util = require('util');
const Odoo = require('node-odoo');
const odoo = new Odoo({
host: '<ip>',
port: 80,
database: 'odoo-db',
username: 'user1',
password: 'test@123'
});
odoo.connect(function (err, result) {
if (err) {
console.log(err);
return;
}
odoo.search('product.template',[],);
});
When execute the line let result = await Util.promisify(odoo.search)('product.template',[]);
I got this error.
TypeError: this._request is not a function
at Odoo.search (/home/ankur/git/StoneHub/node_modules/node-odoo/lib/index.js:98:7)
at internal/util.js:230:26
at test (/home/ankur/git/StoneHub/commons/services/odoo-client/index.js:39:49)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
When I call stright forward callback it work perfectly.
const Util = require('util');
const Odoo = require('node-odoo');
const odoo = new Odoo({
host: '<ip>',
port: 80,
database: 'odoo-db',
username: 'user1',
password: 'test@123'
});
odoo.connect(function (err, result) {
if (err) {
console.log(err);
return;
}
odoo.search('product.template', [], function(err,result) {
if (err) {
console.log(err);
return;
}
console.log(result);
});
});
Any Idea this is node error or anything I am missing.
First off, this would be better suited to https://github.com/nodejs/help/issues so I'm going to close it out. Now, to the issue at hand:
You're passing a straight up function reference to util.promisify whereas it's really a method on the instance of the Odoo class. It's similar to if you tried to do something like setImmediate(odoo.search, 'product.template', []). It needs a reference to the instance it belongs to function correctly.
I think in general you will want to store your promisified reference somewhere so that you can reuse it later for the same connection. But anyway, here's how you would do it if you just care about getting it to work and not about performance: util.promisify(odoo.search.bind(odoo)).
@ankurloriya
When you directly call odoo.search() it will use odoo as the this variable when calling search method. But, if you call it indirectly (for example (odoo, odoo.search)()) it will use different this. One way to fix it is to pass another function which will call your main function, like this:
Util.promisify((...args) => odoo.search(...args))('product.template', []);
Most helpful comment
@ankurloriya
When you directly call
odoo.search()it will useodooas thethisvariable when callingsearchmethod. But, if you call it indirectly (for example(odoo, odoo.search)()) it will use differentthis. One way to fix it is to pass another function which will call your main function, like this: