Issue with Promise function not resolving in the context of javascript and Wit.ai

Currently, I am in the process of updating the functions in my messenger/wit.ai chat bot to transition from using callbacks to promises.

The initial format functions properly:

['buildScenario'](sessionId, context, cb) {

    var trendChoice = scenarioCombos['trends']
    var disruptionChoice = scenarioCombos['disruptions']
    context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
    context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]

    cb(context)
},

However, after updating to Promises as shown below, it encounters issues:

['buildScenario']({sessionId, context, entities}) {
    return new Promise(function(resolve, reject) {
        var trendChoice = scenarioCombos['trends']
        var disruptionChoice = scenarioCombos['disruptions']
        context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
        context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
        return resolve(context)
    })
},

I have attempted debugging by adding console logs within the function like this:

https://i.sstatic.net/FCdHL.png

Upon triggering the function, it halts midway and fails to fulfill the promise:

https://i.sstatic.net/vB3Jo.png

When I try console.log(context) inside the function, 'undefined' is returned.

Is there something I overlooked?

EDIT: When I remove the curly brackets around my function parameters as follows:

['buildScenario'](sessionId, context, entities) {
    console.log('BS POINT 1')
    return new Promise(function(resolve, reject) {
        console.log('BS POINT 2')
        var trendChoice = scenarioCombos['trends']
        console.log(trendChoice)
        console.log('BS POINT 3')
        var disruptionChoice = scenarioCombos['disruptions']
        console.log(disruptionChoice)
        console.log('BS POINT 4')
        console.log(context)
        context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
        console.log(context)
        console.log('BS POINT 5')
        context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
        console.log(context)
        console.log('BS POINT 6')
        return resolve(context)
    })
},

I can log my context successfully but still encounter resolution issues with the Promise:

https://i.sstatic.net/m8WFN.png

Answer №1

Your buildSenario should ideally resemble this structure, but it's not a problem if you prefer to use it differently. Feel free to disregard it as it is just a warning message.

buildScenario({{sessionId, context, text, entities}}) {});

It appears that some of your callbacks or Promises are not resolving within the 10-second timeframe.

I have experience working with wit.ai on Facebook bot integration, where the Messenger bot node must send a 200 status when receiving a text message. Take a look at this code snippet for reference.

Example of wit.ai integration with Facebook Messenger

Answer №2

I discovered that my package dependencies were restricting the node-wit API version to 3.3.2, preventing it from updating to newer versions (specifically, v4.0.0 which switched to Promise-based API instead of callbacks). After editing my package.json file to allow for the latest node-wit version, I was able to successfully implement it.

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

Issue with Magento site: Clicking on thumbnail photo does not update the large photo on mobile devices

As I navigate through a Magento site, I encounter a problem with the thumbnail images not reloading the larger image when clicked. Instead, the browser jumps to the top of the page and a hash symbol gets added to the URL. Based on this behavior, I suspect ...

Field labels remain visible even when corresponding input fields or selection options are hidden

I have implemented a Bootstrap form with the form-floating class on the controls. An issue arises when certain controls are hidden, causing the labels to remain visible and become congested. Oddly enough, this only occurs with some controls while others b ...

Guide to flattening a nested array within a parent array using lodash

Within my array named sports, there is another array called leagues, but I need to flatten these arrays using _.flatten due to issues with Angular filters. If you look at the data, inside the first array which contains a "name":"Live Betting", there is an ...

What is the best way to access a value within a JSON object in a React render method?

Overview I am currently working on creating a blog using React and JSON data. Upon checking this.state.blogs, I am getting the output of: [object Object],[object Object],[object Object]. I have attempted to use the .map function but I am not sure how to ...

Ajax - Every single request has been successfully fulfilled

I am struggling to determine when all my Ajax requests are finished in order to call another function. I have attempted to use various methods with native Ajax functions in Chrome, but none of them seem to be working for me. Can someone please help me? H ...

Alternative for document.ready in AngularJS when outside of AngularJS

I am currently developing a small Chrome extension that will interact with an Angular website. I have managed to successfully detect full page reloads using $(document).ready(), but I am facing issues when it comes to detecting page changes triggered by ng ...

Disallow Zooming and Panning Chart Beyond the Viewport Bounds in d3 v2

I've been scouring through various examples trying to implement this feature in my chart, but with no luck so far. While I have managed to enable panning and zooming on the chart, the issue arises when I can pan and zoom beyond the intended viewport ...

Unable to clear JSP form data

While attempting to reset the form data in JSP, the code given below is run. Although the data gets reset successfully, it reappears mysteriously from an unknown source: $('input[type="text"]').val(''); ...

Looking to sanitize an array of objects in Node.js? If you find that manually iterating through it only returns 'object Object', there are alternative methods to properly

I have a collection of items structured like this: var data = [ { msg: 'text' }, { src: 'pic.jpg',id: 21,title: 'ABC' } ]; My goal is to cleanse the values by manually iterating throug ...

Ways to determine whether JavaScript is enabled in the browser using .ASPX

One way to potentially reduce form spam on our site is by considering the impact of javascript. Spammers typically do not have javascript enabled when accessing websites, so this could be a quick and effective solution to lower the amount of spam we rece ...

guide on incorporating Google Maps in a Vue.js application

Can anyone help me with displaying a Google Map using Vue.js? I have provided the code below, but I keep getting an error saying "maps is undefined" even though I have installed all the necessary dependencies for Google Maps. <div id="map"></div& ...

Is it feasible in AngularJS to compel a view to refresh without using the controller associated with it?

UPDATE: The title question has been correctly answered below. In order to provide a helpful example, I will keep the original question along with the answer below. My current framework does not completely abstract the Angular variable-to-camelcase conven ...

Is there a way to showcase a countdown timer on an Angular Toaster?

I am utilizing the ng-Idle and Angular Toaster modules in my project. I aim to exhibit a toaster notification during the IdleWarn event of ng-idle, along with the countdown value. $scope.$on('IdleWarn', function(e, countdown) { console.log ...

What is the best way to extract the text from the first visible `<td></td>` table row using jQuery?

In this particular scenario, there is a table included: <table id="table"> <thead> <tr> <th>Name</th> <th>Course</th> </tr> </thead> <tbody> <tr style="display:none"> ...

Unraveling the mystery of "??=" in Javascript/Typescript code

In a recent TypeScript code snippet, I came across the following: const arrayAA: Record< someSchema['propX'], typeof arrayBB > = {}; for (const varB of arrayBB) { (arrayAA[someStringValue] ??= []).push(varB) } What is ...

I am experiencing technical difficulties with my API resulting in a 500 Internal Server Error

My application involves managing products using CRUD operations. I am able to successfully make an HTTP request to the /api/deleteProduct route in order to delete a product based on its ID. However, the issue lies with creating a new product as the functi ...

Combine two objects while keeping only specific properties

Currently facing a challenge with merging two objects in the desired way using Node.js express and MongoDB. Here are the initial objects: Obj1: { "name":"max", "age":26, "hometown": & ...

How can I properly execute json requests and save data when iterating over an array?

I'm currently trying to loop through an array of addresses in order to request their respective geolocations using the Google Maps Geocode API. However, I'm facing an issue with pushing the results (latitude and longitude coordinates) into an arr ...

The function recipes.map is not compatible with ReactJS

I am facing an issue while trying to display elements from an array as components in ReactJS. The error message 'recipes.map is not a function' keeps popping up when I execute my code. Below is the snippet of my code: fetchRecipes() { va ...

Prevent variable declaration using try-catch blocks

I am currently working on a code that aims to retrieve a path value from an XML file. If the path is not found, I want to set it to blank. Interestingly, I encounter errors when I use try/catch blocks, stating that the "path" cannot be found. However, whe ...