The output from the second request using RxJS

I am facing an issue with returning an Observable from the second request. Here is the scenario I am encountering:

commandRequest(action:string, commandData:any):Observable<CashDesckResponse>
{
    let command:CashDeskRequest;
    //ask my backend for command
    this.requestCommandString(action, commandData, "POST")
        //this is my response from backend
        .map(r=>r.response).subscribe(my_1_response=>{
        //then i need to send this response data to other server/action 
        //to execute it and return this second response as the result of
        //this method.
        command = new CashDesckRequest(my_1_response);
        return this.server.executeCommand(command).map(return_this=>return_this);
    }); 
};

private requestCommandString(action:string, requestData:any,
                             type:string):Observable<AjaxResponse>{
    return Observable.ajax({
        body:JSON.stringify(requestData),
        method:type,
        url:this._apiServerUrl+action
    }).map(r=>r);
};

The problem arises when trying to return a value from the inner subscribe function. The compiler throws an error stating: [ts] A function whose declared type is neither 'void' nor 'any' must return a value.

Answer №1

If you want to transform one stream into another, you have the option to utilize mergeMap or switchMap.

commandRequest(action:string, commandData:any):Observable<CashDesckResponse>
{
    return this.requestCommandString(action, commandData, "POST")
        .map(r1 => new CashDesckRequest(r1.response)
        // map first response to command instance
        .mergeMap(command =>this.server.executeCommand(command));
        // map the command observable to a new observable
}

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

Updating Rails Partial with JSON Data

Updating a partial by appending fetched Facebook posts using the koala gem requires some code adjustments. Here is an example of how to achieve this: # feed_controller.rb def index end def fb_feed @fb_feed = .. respond_to do |format| format.js { ...

Unloading a dynamically-loaded child component in Vue.js from the keep-alive cache

I have a question that is similar to the one mentioned here: Vue.js - Destroy a cached component from keep alive I am working on creating a Tab System using Vue router, and my code looks something like this: //My Tab component <template> <tab& ...

Error: Import statement not allowed outside a module when using Material UI

I am relatively new to material UI. In my React project, I am incorporating material UI components. I am trying to implement a customized radio button following the instructions in the Mui documentation: https://mui.com/material-ui/react-radio-button/#cust ...

Having trouble getting a Custom React Component from an external library to work with MUI styling

I am currently working with a React component that comes from a module located in the node_modules folder: type Props = { someProps: any }; export function ComponentA(props: Props) { const [value, setValue] = React.useState(""); return ( <Te ...

Unable to process jquery AJAX request

Hello, I've been attempting to make a simple ajax call to a method in my code behind. Despite keeping it straightforward for testing purposes, I keep encountering errors. It seems like a basic mistake, but I'm at a loss on how to proceed. The pro ...

utilize jquery to submit numerous forms using ajax

If I have 3 forms, each with its own submit button: <form name="form1"> <input name="input"></input> <submit></submit> </form> <form name="form2"> <input name="input"></input> <submit></submi ...

What is the best way to create a props interface that includes state and setState values that are being

As I dive deeper into TypeScript, a hurdle has appeared in my app. Upon introducing React.FunctionComponent to my components, an issue arose with my Props interface. The error message reads: Type '({ value, setValue, }: Props) => JSX.Element' ...

My ajax jquery call does not receive a response from the POST REST Service, even though it works fine with the GET request

I've built a basic REST server using Jersey (in Java) with a web interface that utilizes jQuery. The server has a REST Service for both GET and POST methods. However, I'm encountering an issue with the POST call. It involves sending a JSON object ...

Why aren't enums that can be derived supported?

Is there a way to create an enum that is a subset of another enum? Sometimes it would be useful to have an enum that is a subset of another Enum with the same values at run time but under a different name. Are there better ways to handle this scenario? ...

When using the `.push` method, the array becomes null

Currently, I am in the process of developing an angular application. Within my component's .ts file, there exists an array structured as follows: public myArray = []; public dataFromAPI = []; In a particular method within this component, whenever I ...

Issue with Typescript and react-create-app integration using Express

I'm relatively new to Typescript and I decided to kickstart a project using create-react-app. However, I encountered an issue while trying to connect my project to a server using express. After creating a folder named src/server/server.ts, React auto ...

Stop unauthorized entry into JavaScript files

Is there a method to secure JavaScript files from unauthorized access? <script src="JS/MyScript.js" type="text/javascript"> It is crucial that users are unable to access the MyScript.js file. I am seeking advice on how to achieve this. Is it even ...

Different Types of Forms on a Single Webpage Using AJAX

I'm struggling to get multiple forms to work on the same page. No matter what I try, it always submits the first form. The goal is to identify each form uniquely so that only the specific one is submitted. The current code below achieves this but alw ...

Utilizing Ajax to Dynamically Update Link Styles

When using an AJAX request to load a portion of a webpage, the content is delivered by a framework and then inserted into the DOM tree using jQuery. Everything seems to be working well so far. However, I encountered an issue when trying to use background ...

When using Ajax in Jquery and expecting data of type JSON, the response always seems

Looking to create a basic jQuery Ajax script that checks a user's discount code when the "Check Discount Code" button is clicked? Check out this prototype: <script> jQuery(function($) { $("#btn-check-discount").click(function() { c ...

What are the best practices for integrating Qt with React in TSX?

While I've figured out how to communicate qt with JS successfully, the challenge arises when trying to use React in TSX for frontend development. The previous solution failed on this front. Backend code: #./main.py import os from PySide6.QtWidgets ...

Obtain JSON data response in PHP with the help of jQuery

Below is the code I am using to make an AJAX call and retrieve data from another PHP file: $.post('<?php echo get_site_url(); ?>/ajax-script/',{pickup:pickup,dropoff:dropoff,km:km}, function(data){ $('#fare'). ...

Converting HTML templates into AMD modules using grunt-ts

When using the grunt-ts plugin and specifying the html: ["*.tpl.html"] option, it converts *.tpl.html files into *.tpl.html.js files at the end of the process, creating a global var. Is there a way to configure grunt-ts to output the final .js file in a d ...

How to use jquery and ajax to retrieve an array of data and show it on the screen

I am facing an issue with my ajax request. Actually, I am unsure of how to fetch multiple records. I attempted the following: $rqt = "SELECT a,b,c from table"; $res = mysql_query($rqt); while ($data = mysql_fetch_assoc($res)): $objet = $d ...

Developing a databound listview in Ionic: A step-by-step guide

In the world of programming, each platform has its own way of handling lists. For example, Android uses RecyclerView, WPF uses ListView, and in Ionic, we have ion-list. If you have a list of strings like this: Animals:string[] = ["Dog", "Cat", "Human", "C ...