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

Error in Typescript syntax within a CommonJS/Node module: Unexpected colon token found in function parameter

After validating the file with TS, there are no more errors. However, during runtime, I encounter an "Unexpected token ':'" error on any of the specified TS, such as immediately erroring on function (err: string). The following are my build and ...

Encountered an error while trying to access the 'touched' property of undefined within Angular6 reactive forms

When attempting to validate my page, I encountered an error stating 'Cannot read property 'touched' of undefined'. Can someone please assist me with this code? Also, feel free to correct any mistakes you may find. Here is the HTML code ...

What is the process for specifying a method on a third-party class in TypeScript?

I'm facing a challenge while trying to extend a third-party class in TypeScript. The issue is that I am unable to access any existing methods of the class within my new method. One possible solution could be to redeclare the existing methods in a sep ...

Ajax fails to transmit information

Currently, I am in the process of familiarizing myself with the usage of ajax. An issue that I am encountering is that clicking a submit button in a form does not effectively send data. Below you can find the JQuery code I am using: $('input[name=" ...

Using query parameters in Angular to interact with APIs

One scenario involves a child component passing form field data to a parent component after a button press. The challenge arises when needing to pass these fields as query parameters to an API endpoint API GET /valuation/, where approximately 20 optional p ...

Is there a way to enable previous days in a UI datepicker after switching to a different month?

A different question can be found at: How can I modify the UI datepicker after switching months? In contrast to Nick Craver's solution, I encountered a separate issue: While Nick Craver sourced his dates from the js variable xml, my dates are retr ...

Keeping information saved locally until an internet connection is established

I have a vision to develop a Web app focused on receiving feedback, but the challenge lies in the fact that it will be utilized on a device without an internet connection. The plan is for it to save any user input offline until connectivity is restored. Th ...

Obtain a union type using the `keyof typeof` syntax

Is there a way to retrieve the union or enum type from a typeof type in TypeScript? For instance: const myConfs: { [k: string]: (myArg: { name: string }) => string } = { 'Hello': ({ name }) => `World from ${name}`, 'Goodbye': ...

Retrieve the information located within the error section of an Ajax request

When sending an ajax request to a Django view, I am including data in the response based on certain logic. return JsonResponse(content={ "Message": "user is not present.", },status=400) This response falls under the error section ...

Challenges with AJAX, Search Engine Optimization, and organizing PHP files

Currently, I am working on an AJAX script that is designed to read a specific file identified by a GET variable. The script begins in index.php: var expFile = '<?php echo $_GET['text_name']; ?>'; $(document).ready(function () { ...

I am unable to utilize autocomplete with types that are automatically generated by Prisma

Currently, I am working on a project utilizing Next and Prisma. Within my schema.prisma file, there are various models defined, such as the one shown below: model Barbershop { id String @id @default(uuid()) name String address String ...

The callback function of the $.ajax statusCode method does not receive any parameters

As per the official jQuery documentation: If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback. However, when I use th ...

Mixing Jest and Cypress in a TypeScript environment can lead to Assertion and JestMatchers issues

When utilizing [email protected] alongside Jest, we are encountering TypeScript errors related to Assertion and JestMatchers. What is the reason for these TypeScript errors when using Jest and [email protected] in the same project? ...

Tips for updating the font size of your MUI Accordion title

I was attempting to adjust the font size of the MUI-5 Accordion title. It seems like I need to override it, but I am unsure about how to do so with CSS in MUI-5. Instead of 'SX', it uses 'htmlSx'. When I tried using it, it did not produ ...

Updating URL parameters with an AJAX request in Rails 3 is a great way to dynamically

I am working with a filter and a list of products that have an id, name, and creation date. The filtering can be done by id, name, or creation date. When I make an AJAX request to update the content div, the URL stays the same. How can I add parameters t ...

Combining Rxjs map and filter to extract countries and their corresponding states from a JSON dataset

I have a unique dataset in JSON format that includes information about countries and states. For example: { "countries": [ { "id": 1, "name": "United States" }, { "id": 2, "name": "India" }], "states": [ { ...

Load nodes for jsTree based on the results of a query when requested by the user

I've been researching extensively, but I have yet to discover the correct solution. I am interested in learning how to dynamically generate a jsTree where the nodes are loaded from a database. The data will be retrieved by a function. My objective i ...

What is the best way to center align the placeholder in an ion-input field?

What is the best way to center align the placeholder in ion-input? Here's a screenshot of my current ionic input fields with left alignment. I've attempted to move the alignment to the center, but I've been unsuccessful. Can someone please ...

Tips on updating the datepicker format to be dd/mm/yyyy in ngbdatepicker

I am currently using ng-bootstrap for a datepicker and need to change the date format from yyyy/mm/dd to dd/mm/yyyy. I have tried to make this adjustment but haven't had success. If anyone has suggestions on how to accomplish this, please help. Here ...

Develop a loading modal for all AJAX requests, with the exception of a specific one

I've implemented a code snippet to display a modal for ajax requests: $(document).ajaxStart(function () { showLoadingModal(); }); $(document).ajaxError(function () { closeLoadingModal(); alert("error"); }); Although this code creates a modal ...