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;
        let sLatitude = '';
        let sLongitude = '';
            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);
            /* 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})
                    /*eslint-disable no-console*/
                    console.log('RESULT : ',result);
                    /*eslint-disable no-console*/
                    console.log('error : ',error);
            //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.


4 Answers 4


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:


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


    return new Promise((resolve, reject) => {
        navigator.geolocation.getCurrentPosition(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.


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

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 //
  • 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 Sep 14, 2019 at 18:37

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


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.

  • 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. Sep 14, 2019 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .