Odoo external api user check in database using Nodejs and xmlrpc

Viewed 31

so i wrote this function to check weather the login given exists already in the odoo database, but it always returns undefined, my guess is that it returns undefined because the return line is inside the calling method,

i tried to use it as an async function but it didn't work either, i need to know how can i make the return line refrences to the global function scope and not just the calling method.(i need the calling method to get the full list of users from the odoo database). Any suggestions ??

`

function user_exist(email){

    odoo.connect(function (err) {
        if (err) { return console.log(err); }
        console.log('Connected to Odoo server.');
        var inParams = [];
        inParams.push([['active', '=', true]]);
        var params = [];
        params.push(inParams);
        
        
        // 4- Read
       
        odoo.execute_kw('res.users', 'search', params, function (err, value) {
            if (err) { return console.log(err); }
            var inParams = [];
            inParams.push(value); //ids
            inParams.push(['login']);
            var params = [];
            params.push(inParams);
            
            odoo.execute_kw('res.users', 'read', params, function (err2, value) {
                if (err2) { return console.log(err2); }
                
                
              
                
                for (let i = 0; i < value.length; i++) {
                    if (email == value[i].login){
                        return "User exist" 
                        
                        
                        
                        
                    }
                    
                  }
                  return "user doesn't exist"
                  
                  
                  
                
            });
            
        });    
        
    });
    

    
}

`

1 Answers

if you are making your api you need to learn how to use postman, and in this solution we are using axios:

var axios = require('axios');

let data = JSON.stringify({
    "jsonrpc": "2.0",
    "method":"call", 
    "params": {
        "service":"object",
        "method":"execute",

        //arg1 : your database name must be "string"
        //arg2 : id of the user admin must be "int" ex:1 or 3 or 66 
        //arg3 : password of the user admin must be "string"
        // admin:admin do like this 2:"admin"
        //arg4 : object or model name ex:"res.users"
        //arg5 : orm methods ex:"search,search_read..."

        //"args":["arg1",'arg2',"arg3","arg4","arg5"]}
        "args":["app",2,"admin","res.users","search_read",[],[]]
     }
});

let config = {

     method: 'get',
     url: 'http://localhost:8069/jsonrpc',
     headers: {'Content-Type': 'application/json'},
     data : data

};

axios(config).then(response => {handleResult(response)})
function handleResult(data) {
    // if you want you can remove result
    console.log(JSON.stringify(data.data.result));
}

i hope this will be help you

Related