Overslaan naar inhoud
Odoo Menu
  • Aanmelden
  • Probeer het gratis
  • Apps
    Financiën
    • Boekhouding
    • Facturatie
    • Onkosten
    • Spreadsheet (BI)
    • Documenten
    • Ondertekenen
    Verkoop
    • CRM
    • Verkoop
    • Kassasysteem winkel
    • Kassasysteem Restaurant
    • Abonnementen
    • Verhuur
    Websites
    • Websitebouwer
    • E-commerce
    • Blog
    • Forum
    • Live Chat
    • E-learning
    Bevoorradingsketen
    • Voorraad
    • Productie
    • PLM
    • Inkoop
    • Onderhoud
    • Kwaliteit
    Personeelsbeheer
    • Werknemers
    • Werving & Selectie
    • Verlof
    • Evaluaties
    • Aanbevelingen
    • Wagenpark
    Marketing
    • Sociale media-marketing
    • E-mailmarketing
    • Sms-marketing
    • Evenementen
    • Marketingautomatisering
    • Enquêtes
    Diensten
    • Project
    • Urenstaten
    • Buitendienst
    • Helpdesk
    • Planning
    • Afspraken
    Productiviteit
    • Chat
    • Artificial Intelligence
    • IoT
    • VoIP
    • Kennis
    • WhatsApp
    Apps van derden Odoo Studio Odoo Cloud Platform
  • Bedrijfstakken
    Detailhandel
    • Boekhandel
    • kledingwinkel
    • Meubelzaak
    • Supermarkt
    • Bouwmarkt
    • Speelgoedwinkel
    Horeca & Hospitality
    • Bar en café
    • Restaurant
    • Fastfood
    • Gastenverblijf
    • Drankenhandelaar
    • Hotel
    Vastgoed
    • Makelaarskantoor
    • Architectenbureau
    • Bouw
    • Vastgoedbeheer
    • Tuinieren
    • Vereniging van mede-eigenaren
    Consulting
    • Accountantskantoor
    • Odoo Partner
    • Marketingbureau
    • Advocatenkantoor
    • Talentenwerving
    • Audit & Certificering
    Productie
    • Textiel
    • Metaal
    • Meubels
    • Eten
    • Brouwerij
    • Relatiegeschenken
    Gezondheid & Fitness
    • Sportclub
    • Opticien
    • Fitnesscentrum
    • Wellness-medewerkers
    • Apotheek
    • Kapper
    Diensten
    • Klusjesman
    • IT-hardware & ondersteuning
    • Zonne-energiesystemen
    • Schoenmaker
    • Schoonmaakdiensten
    • HVAC-diensten
    Andere
    • Non-profitorganisatie
    • Milieuagentschap
    • Verhuur van Billboards
    • Fotograaf
    • Fietsleasing
    • Softwareverkoper
    Alle bedrijfstakken bekijken
  • Community
    Leren
    • Tutorials
    • Documentatie
    • Certificeringen
    • Training
    • Blog
    • Podcast
    Versterk het onderwijs
    • Onderwijsprogramma
    • Scale Up! Business Game
    • Odoo bezoeken
    Download de Software
    • Downloaden
    • Vergelijk edities
    • Releases
    Werk samen
    • Github
    • Forum
    • Evenementen
    • Vertalingen
    • Partner worden
    • Diensten voor partners
    • Registreer je accountantskantoor
    Diensten
    • Vind een partner
    • Vind een boekhouder
    • Een adviseur ontmoeten
    • Implementatiediensten
    • Klantreferenties
    • Ondersteuning
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Vraag een demo aan
  • Prijzen
  • Help
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Help

Javascript query [ deferred values ] - how to get them?

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
2 Antwoorden
7947 Weergaven
Avatar
Rui Franco

Taking in consideration the following code, how can I populate the 'lat' and 'lng' variables? I can only get a value inside the "query". :|

 

var lat=0;

var lng=0

if (coords){
                var coords_obj = new openerp.Model('gps_base.coords');
                coords_obj.query(['latitude_aux', 'longitude_aux'])
                     .filter([['id', '=', coords]])
                     .limit(1)
                     .all().then(function (result) {
                            _.each(result, function(item) {
                                lat=item.latitude_aux;
                                lng=item.longitude_aux;
                            });
                        });
                alert(lat); //shows 'undefined'
            }

0
Avatar
Annuleer
Avatar
Shawn Varghese
Beste antwoord

From my understanding of jQuery, .then() is an asynchronous call. This means that, the .then() function will be called after the coords_obj query returns a result, but any code outside will be carried out without waiting for query result.

The following line:

alert(lat);

, is outside the .then() function. Therefore, it gets called before the query returns a result. But the wierd part is that it doesn't even show the default value 0 . I noticed that console.log(lat) works in such cases, instead of using alert(lat).

Try modifying the code like this:

 

if (coords){

                var coords_obj = new openerp.Model('gps_base.coords');
                coords_obj.query(['latitude_aux', 'longitude_aux'])
                     .filter([['id', '=', coords]])
                     .limit(1)
                     .all().then(function (result) {
                            _.each(result, function(item) {
                                lat=item.latitude_aux;
                                lng=item.longitude_aux;

self.latLngFunction(lat,lng); //This way, lat and lng are displayed correctly


                            });
                        });
            }
,

// After the above function body, create a new function similar to the one below:

latLngFunction:function(lat,lng)
        {
            console.log("Latitude",lat); //
You can view this result in your browser console
            console.log("Longitute",lng);
            //
OR
            alert(lat); //Now the alert should display a proper value.
            alert(lng);
        },

 

0
Avatar
Annuleer
Shawn Varghese

Please let me know if this doesn't work.

Shawn Varghese

The deferred and resolve calls that you have used seem correct. This is the cleaner way to do it. However, I have faced issues with using these calls in the past - I think I am missing something as well. Can you try using console.log(query_res.latitude_aux); instead of alert(query_res.latitude_aux), just to confirm that the issue does not reside with the alert() dialog.

Avatar
Rui Franco
Auteur Beste antwoord

Many thanks Swahn but no, it didn't work. You did set me in the proper direction and, after looking for widget code in the Odoo modules, I eventually came to write this:

          function get_coordinates(coords_id){

                var result = new $.Deferred();
                new instance.web.Model('gps_base.coords')
                    .query(['latitude_aux', 'longitude_aux'])
                    .filter([["id", "=", coords_id]])
                    .first()
                    .then(function(records){
                        if(!records || records.length === 0){
                            result.reject();
                        }else{
                            result.resolve(records);
                        }
                    });
                return result;
            }

 

            if (coords){
                query_res=get_coordinates(coords);
                alert(query_res.latitude_aux);
            }

 

But, unfortunately, I never get a proper value. I'm lost. It seems no matter what example I follow, it never works. Has to be something really silly I am missing here... :(

0
Avatar
Annuleer
Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!

Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!

Aanmelden
Community
  • Tutorials
  • Documentatie
  • Forum
Open Source
  • Downloaden
  • Github
  • Runbot
  • Vertalingen
Diensten
  • Odoo.sh Hosting
  • Ondersteuning
  • Upgrade
  • Gepersonaliseerde ontwikkelingen
  • Onderwijs
  • Vind een boekhouder
  • Vind een partner
  • Partner worden
Over ons
  • Ons bedrijf
  • Merkelementen
  • Neem contact met ons op
  • Vacatures
  • Evenementen
  • Podcast
  • Blog
  • Klanten
  • Juridisch • Privacy
  • Beveiliging
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk Slovenščina Español (América Latina) Español Svenska ภาษาไทย Türkçe українська Tiếng Việt

Odoo is een suite van open source zakelijke apps die aan al je bedrijfsbehoeften voldoet: CRM, E-commerce, boekhouding, inventaris, kassasysteem, projectbeheer, enz.

Odoo's unieke waardepropositie is om tegelijkertijd zeer gebruiksvriendelijk en volledig geïntegreerd te zijn.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now