1

i'm trying to call a back-end apex method imperatively which returns the weather information for the logged in user's location and it throws the following error in console :

error : Error: newValue cannot be undefined. Observable name: MapWithFilterOnSubscribeBehaviorSubject <= DistinctUntilChanged <= RootObservable: lds.Apex::WeatherData:getWeatherData:false:{"sLatitude":"","sLongitude":""}, newValue: undefined

Below is the js code:

 import { LightningElement , track} from 'lwc';
import WEATHER_DATA from '@salesforce/apex/WeatherData.getWeatherData';
export default class UserLocation extends LightningElement {
    @track sLatitude;
    @track sLongitude;
    connectedCallback(){
        let sLatitude = '';
        let sLongitude = '';
        if(navigator.geolocation){
            navigator.geolocation.getCurrentPosition(function(position) {
                sLatitude = position.coords.latitude;
                /* eslint-disable no-console */
                console.log('position  latitude : ' ,sLatitude);
                sLongitude = position.coords.longitude;
                /* eslint-disable no-console */
                console.log('position  longitude : ' ,sLongitude);
              });
        }else{
            /* eslint-disable no-console */
            console.log('inside else');
        }
        /* eslint-disable no-console */
        console.log('position  latitude 11: ' ,sLatitude);
        if((sLatitude !== undefined && sLatitude !== '') && (sLongitude !== undefined && sLongitude !== '')){
            /* eslint-disable no-console */
            console.log('Will call backend from here  ');
            WEATHER_DATA({sLatitude : sLatitude , sLongitude: sLongitude})
                .then(result=>{
                    /*eslint-disable no-console*/
                    console.log('RESULT : ',result);
                })
                .catch(error=>{
                    /*eslint-disable no-console*/
                    console.log('error : ',error);
                })
        }else{
            //Do something here 
        }
    }
}

It gives the following console messages :

enter image description here

What's weird here is the order in which the console messages are printed. This seems to be violating the order of execution somehow and I'm not sure why it's happening.

Regarding the back-end code, it's working fine as I have already tested it by passing the latitude and longitude values through anonymous window.

Can someone help me understand and solve this.

3

I believe you need to slightly redesign this to behave as you're expecting it to. The way I'm reading this, your back-end call will never fire because your geolocation variables will not be set by the time it gets there. Here's a quick example:

connectedCallback(){

    this.getLocation().then(res => {
        // process coordinates/perform back-end call here
    })

}

getLocation(){
    return new Promise((resolve, reject) => {
        navigator.geolocation.getCurrentPosition(position => {
            resolve(position);
        });
    })
}

This pattern will ensure the getCurrentPosition callback is executed before you perform your back-end call. This function is asynchronous so you need to handle it accordingly.

|improve this answer|||||
4

Your Apex appears to be crashing and/or not returning a value, resulting in the aforementioned error. As a related note, your method is being called inappropriately because your parameters are blank. The if statement is incorrect:

a !== undefined || a !== ''

This statement is always true. This is because of the truth table for OR:

             Left Side
             FALSE TRUE
Right  FALSE FALSE TRUE
Side   TRUE  TRUE  TRUE

In other words, if a is undefined, it will not be an empty string. You should be using && to compare one variable to multiple values.

As an aside, it's usually fine to just say:

if(a && b)

Because undefined, empty string, and zero are all evaluated as false, otherwise they are not-false (true).

if(sLatitude && sLongitude) {
  // do the rest here //
|improve this answer|||||
  • I have updated the code, it does not enter the concerned if where I check for values to be blank or undefined. The order of execution seems to be confusing me as mentioned in the updated question. Any idea why this might be happening – Ashutosh Arora Sep 14 '19 at 18:37
0

Make sure you don't use (Cacheable=true). That's was the sulotuin in my case.

|improve this answer|||||
-1

I haven't seen this type of error message before but suggest you also check that you have set the return properties/members as (at)AuraEnabled in your Apex code. Missing this on the newValue would mean this is always undefined on the client.

|improve this answer|||||
  • the back-end functionality seems to be working fine as I've already tested it using anonymous window. The back-end method itself is not getting called as nothing is coming in debug logs on load of the above component. – Ashutosh Arora Sep 14 '19 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.