JavaScript this becomes null inside the javascript function c++ this pointer becomes null

I have java script function which gives current lat long inside that function making an http post call like this "this.http.post" but the value of this null inside the function

My code

ngOnInit(): void {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(p) {
          var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
          console.log(p.coords.latitude);
          console.log(p.coords.longitude);
          var dataObj = {
            latitude: p.coords.latitude,
            longitude: p.coords.longitude
          };
          this.http.post('https://XXXXX/datacenteres.php', {
              dataObj
            })
            .subscribe(
              res => {
                console.log(res);
              },
              err => {
                console.log("Error occured");
              }
            );
        }
      }
    }

I am getting this errors in the console "Cannot read property 'http' of null".

Inside navigator.geolocation.getCurrentPosition(function (p) { this is null }

Inside the If statement i am able to get the "this".

Answer:1

Try like this :

use .bind(this) end of the function

ngOnInit(): void {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(p) {
            var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);

            console.log(p.coords.latitude);
            console.log(p.coords.longitude);

            var dataObj = {
                latitude: p.coords.latitude,
                longitude: p.coords.longitude
            };
            this.http.post('https://XXXXX/datacenteres.php', {
                dataObj
            }).subscribe(res => {
                console.log(res);
            }, err => {
                console.log("Error occured");
            });
        }.bind(this));

    }
}
Answer:2

You can use arrow function like below:

if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(p => {
        //var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
        console.log(p.coords.latitude);
        console.log(p.coords.longitude);
        var dataObj = {
          latitude :p.coords.latitude,
          longitude:p.coords.longitude
        };
        this.http.post('https://XXXXX/datacenteres.php', {
          dataObj
        })
          .subscribe(
            res => {
              console.log(res);
            },
            err => {
              console.log("Error occured");
            }
          );
      })
    }
Answer:3

I am trying to combine two queries in Mongoose into one. Right now, I'm doing this: User.find({ _id: req.body.userId }, (err, user) => { User.find({ username: decodedUser.username }, (err, ...

I am trying to combine two queries in Mongoose into one. Right now, I'm doing this: User.find({ _id: req.body.userId }, (err, user) => { User.find({ username: decodedUser.username }, (err, ...

  1. execute multiple queries in dbeaver
  2. execute multiple queries in python
  3. execute multiple queries in stored procedure
  4. execute multiple queries in mysql
  5. execute multiple queries in mysql php
  6. execute multiple queries in mysql python
  7. execute multiple queries in java
  8. execute multiple queries postgres
  9. execute multiple queries in oracle
  10. execute multiple queries in sqlite
  11. execute multiple queries in sql server
  12. execute multiple queries in c#
  13. execute multiple queries in hibernate
  14. execute multiple queries in one statement
  15. execute multiple queries in jdbc java
  16. execute multiple queries in php
  17. execute multiple queries in toad
  18. execute multiple queries in access
  19. execute multiple queries on a single connection c#
  20. execute multiple queries in talend

I used this script on Github and edited it a bit: https://gist.github.com/k0nsl/733955a3c3093832de49 Added a ping function over PHP which works fine. I also wanted to split my Servers into 2 tables, ...

I used this script on Github and edited it a bit: https://gist.github.com/k0nsl/733955a3c3093832de49 Added a ping function over PHP which works fine. I also wanted to split my Servers into 2 tables, ...

In my current scene stack, I have 3 scenes A,B and C. Going back and forth from A to B to C works fine. But I couldn't navigate back to scene A directly from scene C. Is this possible? My current code ...

In my current scene stack, I have 3 scenes A,B and C. Going back and forth from A to B to C works fine. But I couldn't navigate back to scene A directly from scene C. Is this possible? My current code ...

  1. react native router flux
  2. react native router example
  3. react native router flux drawer
  4. react native router flux docs
  5. react native router flux vs react navigation
  6. react native router flux tabs
  7. react native router flux scene
  8. react native router flux example
  9. react native router expo
  10. react native router flux lightbox
  11. react native router flux navbar
  12. react native router flux drawer example
  13. react native router flux deep linking
  14. react native router flux backhandler
  15. react native router flux actions is not a function
  16. react native router flux npm
  17. react native router navigation
  18. react native router redux
  19. react native router flux modal
  20. react native router dom

I have an array of arrays of objects as below which is generated by _.filter lodash function which returns objects for the matched values: const matchedPositions = _.filter(allPositions, function(o) {...

I have an array of arrays of objects as below which is generated by _.filter lodash function which returns objects for the matched values: const matchedPositions = _.filter(allPositions, function(o) {...

  1. convert array to array list
  2. convert array to array of objects javascript
  3. convert array to array list java
  4. convert array to array of objects
  5. convert array of array to list python
  6. convert array of array to json
  7. convert array into array list
  8. convert array to array object php
  9. convert array to array
  10. convert array to array javascript
  11. convert array of array to string php
  12. convert array of array to dataframe
  13. convert array to array c#
  14. convert array to array php
  15. convert array into array list java
  16. convert array into array of object
  17. convert array to array string javascript
  18. convert array to array collection
  19. python convert array.array to string
  20. convert array to array list c#