Error: The method of promise.then is not recognized as a function

Having an issue with Rxjs v5 while attempting to run http.get requests one after the other in sequential order. The error message received is

TypeError: promise.then is not a function
. Below is the code snippet in question:

    var http = require('http');
    Rx.Observable
        .from(data)
        .pairwise()
        .concatMap(a => {
            var url = 'http://to/some/api?origins=' + a[0].lat + ',' + a[0].lng + '&destinations=' + a[1].lat + ',' + a[1].lng;
            return Rx.Observable.fromPromise(http.get(url));        
        })    
        .subscribe(item => {
            console.log(item);
        });

Answer №1

The `http.get` method in Node.js does not return a promise, as explained here

Since it uses a non-standard interface, a small custom work-around is needed to make it function properly. Here's a relatively simple implementation:

var http = require('http');
function observableGet(options) {
  return new Rx.Observable(subscriber => {
    var subscription = new Rx.Subscription();

    // Custom logic for making the request and handling data

  });
}

You can then use this custom `observableGet` function instead of `http.get` like this:


Rx.Observable
.from(data)
.pairwise()
.concatMap(a => {
var url = //...url;
return observableGet(url);        
})    
.subscribe(item => {
console.log(item);
});

Alternatively, you could consider using a library that returns Promises, such as request-promise, which may simplify your code.

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

RXJS buffering with intermittent intervals

Situation: I am receiving audio data as an array and need to play it in sequence. The data is coming in continuously, so I am using an observable to handle it. Since the data arrives faster than it can be played, I want to use a buffer to store the data w ...

When a large object changes value in Firebase, does it transfer only the delta change in internet bandwidth?

Hello, I am working with a node that contains an array of 1000 nodes, totaling around 60KB in size. rootRef.on('value', function (snapshot){ //callback every changes props , full list obj }); I would like to know: - When a node in ...

How can I reduce the burden of dependencies on users with a pre-built NPM package?

I recently took over maintenance of an NPM package. It has a unique setup where the main file is located in the 'dist/' directory and it's built using webpack (via 'npm run build'). While this works for us, installing this package ...

Guide to automatically loading a default child route in Angular 1.5 using ui-router

Hello, I am looking to set a default child route to load as soon as the page loads. Below is the code snippet: $stateProvider.state('userlist', { url: '/users', component: 'users', data:{"name":"abhi"}, resolv ...

When an image is clicked, I am attempting to access data from a Sharepoint list

In my Sharepoint list, there are three columns: Image, Title, and Description. I am using ajax to retrieve the items from the list. The images can be successfully retrieved, but my goal is to display the title and description of the clicked image when an ...

Performing an HTTP request response in JavaScript

I am trying to make an HTTP request that returns the data in JSON format using 'JSON.stringify(data)'. var xhr = new XMLHttpRequest(); xhr.open("GET", "/api/hello", true); xhr.send(); xhr.onreadystatechange = function () { console.log(xhr.r ...

What is the best way to utilize ng-if in the index.html page depending on the URL?

Is there a way to hide certain elements in the index page based on the URL of nested views? In my index.html file, I am looking to implement something like this: <top-bar ng-if="!home"></top-bar> <ui-view class="reveal-animation"> ...

Using observables rather than promises with async/await

I have a function that returns a promise and utilizes the async/await feature within a loop. async getFilteredGuaranteesByPermissions(): Promise<GuaranteesMetaData[]> { const result = []; for (const guarantees of this.guaranteesMetaData) { ...

Out of the blue synchronization issues arising from utilizing the nodejs events module

In my code, I am utilizing the Node Events module to execute a function asynchronously. var events = require('events'); var eventEmitter = new events.EventEmitter(); eventEmitter.on('myEvent', f2); function f1(x, y) { console.log( ...

Achieve SEO excellence with Angular 4 in production settings

I am currently building a website using Angular 4 as part of my personal study project. Although my website is live, I realized that it's not SEO friendly. After making some changes, I came across a valuable resource on server-side rendering in Angul ...

Incorporating Nunjucks into Express 4

I recently attempted to integrate Nunjucks as my template engine within my Express application. Here's the code snippet I used: var express = require('express'); var nunjucks = require('nunjucks'); var path = require('path&ap ...

Modify the color of the div element after an ajax function is executed

My original concept involves choosing dates from a calendar, sending those selected dates through ajax, and then displaying only the chosen dates on the calendar as holidays. I aim to highlight these selected dates in a different color by querying the data ...

What is preventing the input box from shrinking further?

I created a search box using CSS grid that is supposed to shrink when the page is resized. However, it doesn't seem to shrink as much as I expected it to. Here is how it appears when fully extended: https://i.stack.imgur.com/tPuCg.png And here is how ...

What are the most effective techniques for combining Vue.JS and Node.js backends into a single Heroku container?

I came across an interesting blog post today that discusses a method for deploying React applications. The author begins with a Node project and then creates a React project within a subfolder, along with some proxy configuration. About 10 days ago, I did ...

A guide on enhancing Autocomplete Tag-it plugin with custom tags

Looking to expand the tags in the 'sampleTags' variable within this code snippet $(function () { var sampleTags = ['c++', 'java', 'php', 'coldfusion', 'javascript', 'asp', &apo ...

Displaying HTML with extracted message form URL

I have a link that redirects to this page Is there a way for me to extract the message "Message Sent Successfully" from the URL and display it in the form below? <form action="send_form_email.php" name="contactForm" method="post"> //I want to d ...

Navigating Parent Menus While Submenus are Expanded in React Using Material-UI

My React application includes a dynamic menu component created with Material-UI (@mui) that supports nested menus and submenus. I'm aiming to achieve a specific behavior where users can access other menus (such as parent menus) while keeping a submenu ...

Issue encountered with Cordova in Visual Studio 2015: Error code bld00401

Having some trouble getting Cordova to work with Visual Studio 2015. Every time I try to open a new project and run the standard template, it fails to build and shows the following errors: Error 1: BLD401 Error: BLD00401: Module C:\Users\Rami K ...

How to properly display an Angular Template expression in an Angular HTML Component Template without any issues?

When writing documentation within an Angular App, is there a way to prevent code from executing and instead display it as regular text? {{ date | date :'short'}} Many sources suggest using a span element to achieve this: <span class="pun"&g ...

Looking to display the "loading....." message on a PHP page?

I am working on a PHP webpage where I need to implement the following features: 1. Upon clicking "Say Thanks", it should change to "Done!". 2. Simultaneously, I would like to trigger an action in the indexController. 3. While this action is happening, I wa ...