Node: Context missised when convert callback to Promise object using Util.promisify

Created on 18 Apr 2018  路  2Comments  路  Source: nodejs/node

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.

  • Version: v8.11.1
  • Platform: Linux ankur-linux-pc 4.14.14-041414-generic #201801201219 SMP Sat Jan 20 12:20:41 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: N/A
wrong repo

Most helpful comment

@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', []);

All 2 comments

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', []);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

loretoparisi picture loretoparisi  路  3Comments

sandeepks1 picture sandeepks1  路  3Comments

srl295 picture srl295  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments