Support Topics Documentation Slack YouTube Blog

loginAsGuest throwing "TypeError: ns.GetCommandSrc is not a function" unless cache is cleared?

Hi Folks - newbie trying to get the guest login functioning to submit scores to a leaderboard. The call works perfectly first time the page loads, but subsequent loads cause this error to throw unless reloaded with cleared cache:

image

Sorry- that posted before I was finished :slight_smile:

(function() {
      const APP_ID = "22D852BB-AD0D-7299-FF0E-DDB46AF3FB00";
      const API_KEY = "313559_PROTECTED_2D00";
      const testTableStore = Backendless.Data.of("sovereign_capital_leaderboard");

      const $inputEmail = document.getElementById("input_email");
      const $inputNickname = document.getElementById("input_nickname");
      const $findBtn = document.getElementById("find-btn");
      const $saveBtn = document.getElementById("save-btn");

      function saveNewObject() {
        const email = $inputEmail.value;
        const nickname = $inputNickname.value;
        const score = Math.round(Math.random() * 10000);
        testTableStore
          .save({ email: email, nickname: nickname, score: score })
          .then(function(savedObject) {
            console.log("save success");
          })
          .catch(function(error) {
            console.log(error);
          });
      }

      function queryTable() {
        var queryBuilder = Backendless.DataQueryBuilder.create();
        queryBuilder.setPageSize(100);
        queryBuilder.setSortBy(["score"]);
        testTableStore
          .find(queryBuilder)
          .then(function(result) {
            console.log(result);
          })
          .catch(function(error) {
            console.log(error);
          });
      }

      Backendless.serverURL = "https://api.backendless.com";
      console.log("inting ");
      Backendless.initApp(APP_ID, API_KEY);
      console.log("attemting login ");
      // Anonymous login without the persistent login option
      Backendless.UserService.loginAsGuest(true)
        .then(function(loggedInGuestUser) {
          console.log("logged in as guest ", loggedInGuestUser);
        })
        .catch(function(error) {
          console.log("error logged in as guest ", error);
        });

      $findBtn.addEventListener("click", queryTable);
      $saveBtn.addEventListener("click", saveNewObject);
    })();

You can see the problem here :

Any ideas???

OK - think I’ve resolved it - Seemingly if I check the user is already logged in, I don’t need the login call:
Backendless.UserService.getCurrentUser() and can make queries successfully. So code now reads:

Backendless.UserService.getCurrentUser()
    .then(function(currentUser) {
      if (!currentUser) {
        Backendless.UserService.loginAsGuest(true)
          .then(function(loggedInGuestUser) {
            console.log("logged in as guest ", loggedInGuestUser);
          })
          .catch(function(error) {
            console.log("error logged in as guest ", error);
          });
      } else {
        console.log("user already logged in");
      }
    })
    .catch(function(error) {
      console.log("error getCurrentUser ", error);
    });

Please correct me if this is not how the guest user API is designed to work.Thanks

apologies for the code formatting - anyone know how to format it nicely?

Hi Dan,

I’ve fixed code formatting - use Preformatted text for this
image

Regarding your problem,
your last code snippet is correct, also I don’t met any problem on your site. So seems the issue is fixed.

For information about how guest login API works please read documentation and feel free to ask if something is not clear.

Regards,
Stanislaw

Thanks for verifying! It all seems to be working now!