Failure to trigger a follow-up function in webSQL transaction's success callback

Review the code below. I have called setQuestion() within the successCallBack of db.transaction but am encountering an error:

Uncaught TypeError: this.setQuestions is not a function
.

Can you spot any issues in my code?

game.module(
"game.scenes.scene"
)
.require(
    "game.assets",
    "game.entities.gameObject",
    "game.entities.backgroundObject",
    "game.entities.animeObject",
    "game.entities.starObject",
    "game.entities.buttonObject"
).body(function() {
  game.SceneScene1 = game.Scene.extend({
    loaded: function() {
         this.get_difficulty_level();
    },
    setQuestions: function() {
        //some code
     },
    get_difficulty_level: function(){
        var app_id = this.app_id;
        var user_id = this.user_id;
        db.transaction(function (transaction)
        {
            transaction.executeSql('SELECT * FROM User_report where app_id="'+app_id+'" and user_id="'+user_id+'" order by id desc limit 1;', [],
            function (transaction, result)
            {
                if (result.rows.length == 0)
                {
                   difficulty_level=2;
                }else{
                     difficulty_level = result.rows.item(0).difficulty;
                     console.log(result.rows.item(0));
                }
            });
          },this.errorHandler,this.successDiffi);
      },
      successDiffi: function(){
          this.setQuestions();
      },
});
});

Answer №1

The reason behind this issue is that the success callback is being executed in a different scope than initially intended. One possible solution to rectify this problem is to implement a closure within the callback:

game.module(
"game.scenes.scene"
)
.require(
    "game.assets",
    "game.entities.gameObject",
    "game.entities.backgroundObject",
    "game.entities.animeObject",
    "game.entities.starObject",
    "game.entities.buttonObject"
).body(function() {
  game.SceneScene1 = game.Scene.extend(
    new function () 
  {
    this.loaded =  function() {
         this.retrieveDifficultyLevel();
    };
    this.setQuestions= function() {
        //some code
     };
    this.retrieveDifficultyLevel= function(){
        var app_id = this.app_id;
        var user_id = this.user_id;
        db.transaction(function (transaction)
        {
            transaction.executeSql('SELECT * FROM User_report where app_id="'+app_id+'" and user_id="'+user_id+'" order by id desc limit 1;', [],
            function (transaction, result)
            {
                if (result.rows.length == 0)
                {
                   difficulty_level=2;
                }else{
                     difficulty_level = result.rows.item(0).difficulty;
                     console.log(result.rows.item(0));
                }
            });
          },this.errorHandler,this.handleSuccess);
      };
      var _this = this;
      this.handleSuccess= function(){
          _this.setQuestions();
      };
});
}); 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

One question I have is how I can successfully send a value within the RxJS subscribe function

I am currently in the process of transitioning the code to rxjs Here is my original code snippet. userAuth$: BehaviorSubject<ArticleInfoRes>; async loadArticleList(articleId: number) { try { const data = await this.articleApi.loadArticl ...

React and Material-UI issue: Unable to remove component as reference from (...)

My React components are built using Material-UI: Everything is running smoothly MainView.js import React, { Component } from 'react'; import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'; import { List, ListItem } from ...

Leveraging JavaScript along with the jQuery library and API to showcase information related to

Hey there! I have been working on this API that shows upcoming concerts, but I'm struggling to display the images associated with each concert. I've tried using for loops to iterate through the objects, and it seems like every sixth element has ...

What is the reason for Firefox displaying the "excessive recursion" error message?

I have been working on generating an area Google chart using the code below, but I am running into an issue where Firefox is showing me a "too much recursion" error. The chart currently only has one point for testing purposes. Can anyone provide some gui ...

The `$(this).data()` function removes any excess data from a database, stopping at the first occurrence of a

One issue I am encountering is when retrieving data with a whitespace. I need the complete string of data, not the trimmed version. When retrieving "data-title" or "data-type", the data gets cut at the whitespace. I tested by setting a static string in ...

My child template in Angular UI is not loading properly due to the presence of multiple views when the URL changes

After reviewing a couple of questions similar to this on various forums, I may have overlooked a crucial detail in them. Upon loading http://localhost:8000/characters/#/mages/detail/3, I am being redirected to my 'otherwise' URL: $urlRouterProvi ...

Making a column in a Vue data grid return as a clickable button

My goal is to utilize vue.js grid to display multiple columns with calculated text values, along with a clickable column at the end that triggers a dynamic action based on a parameter (such as calling an API in Laravel). However, when I include the last c ...

Failed to send JSON data to WebMethod

I am encountering difficulties while attempting to send JSON to a WebMethod. I have provided the method I am using below. If there is a more efficient approach, please advise me. My goal is to store the JSON object in a database. JavaScript function TEST ...

The onload function in jQuery is not functioning properly when the page is refreshed

I'm just starting out with jquery and I have a simple project in mind. The idea is to have two pictures stacked on top of each other, but I want the page to start showing the second picture first (at a specific scroll point). Then as the user scrolls ...

A guide on utilizing Puppeteer for capturing screenshots of web pages with embedded videos

Currently, I am using Puppeteer to access a website and capture a screenshot of a video. Unfortunately, the default Chromium browser that Puppeteer uses does not support certain video types. I have managed to get it working by launching Puppeteer with a l ...

JavaScript barcode data input

I am working with 2 inputs - #barcode and #quantity. When using a barcode scanner, I can easily enter the quantity after scanning data by pressing the Tab key. Typically, the quantity is null, indicating one piece. Currently, my cursor is in the #quantity ...

How can I adjust the time in a range slider using AngularJS?

Currently, I am utilizing a Slider with draggable range in angular js for time selection. The Slider can be found here: https://jsfiddle.net/ValentinH/954eve2L/. I aim to configure the time on this slider to span from 00.00 to 24.00, with a 10-minute inter ...

Error: The middleware function is not recognized | Guide to Transitioning to React Redux Firebase v3

After utilizing these packages for my project, I encountered an error in middleware composition while creating a new react app with create-react-app. Below are the packages I have included. Can someone please help me identify what is missing here? HELP I ...

What could be causing the issue of a Javascript array not filling inside a loop, even though it functions properly

I have a JSON dataset and I'm trying to extract the information of individuals who haven't paid their dues. Initially, when running the code with the if statement commented out, it works perfectly fine - giving the expected output in both the con ...

Adding a modified (id) content inline template element to another element: A step-by-step guide

In the given scenario, I am looking to achieve a function where each time the add button is clicked, the content within the template div should be appended to the element with the landingzone class. Additionally, it is important that the NEWID changes for ...

Calculating the sum of values in a specific position within an array of Javascript

Here is an array that needs to be updated: let arr = [ { "Id": 0, "Name": "Product 1", "Price": 10 }, { "Id": 0, "Name": "Product 1", "Price": 15 } ] I am looking for a way to add 1 to all the Price values, resulting in: let Final_arr = [ { ...

What is the process for loading a font file in Vue.js and webpack?

I've done a lot of research, but I couldn't find any links that show me exactly how to add fonts in VueJS. This is the method I'm using to import the font in my LESS file: @font-face { font-family: "Questrial"; src: url("../../fonts/Que ...

To prevent the background window from being active while the pop-up is open

I have a link on my webpage that triggers a pop-up window, causing the background to turn grey. However, I am still able to click on other links in the background while the pop-up is open. I tried using the code document.getElementById('pagewrapper&ap ...

GraphQL failing to communicate with WP API

Any help is appreciated! I am currently retrieving data from a WP Rest API. When I run the WordPress site locally on my machine using http://localhost:8000 and access the graphql playground at http://localhost:3000/api/graphql, everything works fine as ex ...

Using Ajax script to transfer user information to a php file in order to refresh the modified row in a table

Recently, I created a table in PHP to display certain data. Here's how it looks: <?php echo '<table id="tableteste" class="table table-striped" width="100%">'; echo '<thead><tr>'; echo &apos ...