Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Javascript side getting wanted field value?

By
Andrius Laukavičius
on 8/19/14, 9:32 AM 2,219 views

I have this piece of code:

new instance.web.Model('hr.employee').query(["week_busyness"]).filter([["id", "=", employee_id]]).first().then(function(result){
                return result.week_busyness;

});

It returns field I need, but I can only use it inside that defined function. But I need that field to be used outside function. I just can't seem to understand how to do that..

For example if I assign it to variable like:

test = new instance.web.Model('hr.employee').query(["week_busyness"]).filter([["id", "=", employee_id]]).first().then(function(result){
                return result.week_busyness;

and then:

console.log(test)

it prints function object, not the 'result.week_busyness'. How could I get instead that value?

can you post your console.log(test) output ?...

Prakash
on 8/20/14, 1:31 AM

or give me the output alert("myObject is " + test.toSource());

Prakash
on 8/20/14, 1:39 AM

output is this: Object {state: function, always: function, then: function, promise: function, pipe: function…}

NOD Baltic, UAB, Andrius Laukavičius
on 8/20/14, 1:40 AM

and I can't output with toSource(). It gives this error then - Uncaught TypeError: undefined is not a function

NOD Baltic, UAB, Andrius Laukavičius
on 8/20/14, 1:43 AM

Andrius, Why don't you define a global variable at the top, and then assign your result to that variable. then you can use it anywhere.... "even outside of the scope of function too"

Pawan
on 9/8/15, 1:07 AM
0
Rui Andrada
On 9/7/15, 2:56 PM

You have to define your function outside that context.

Maybe you could try:

function getResults (result) {
console.log(result); // to see which data come from server
    return result.week_busyness;
}
function getError (result) {
console.log(result); // to see which data come from server
}

then you call .Model() which always returns a promise.


test = new instance.web.Model('hr.employee')
       .query(["week_busyness"])
       .filter([["id", "=", employee_id]])
       .first().then(getResults);

or

test = new instance.web.Model('hr.employee')
       .query(["week_busyness"])
       .filter([["id", "=", employee_id]])
       .first().done(getResults).fail(getError);


If you use then(getResults) even if error occur you have to handle the returned data or error.

If you use .done(getResults), the function only called if call to server was ok

If you use .fail() you could catch error if happens.


If you need more information, try search for javascript promises

I hope it's help you.


0

Ben Bernard

--Ben Bernard--
1083
| 4 3 6
Jakarta, Indonesia
--Ben Bernard--
Ben Bernard
On 8/20/14, 2:08 AM

Can you do this

test.done(function( value ) {
    console.log(value);
});

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 8/19/14, 9:32 AM
Seen: 2219 times
Last updated: 9/8/15, 1:07 AM