Guide to fetching the most recently created entry using loopback api and angularjs

In this section here, my Modal controller is outlined with Se_chnl and Se_segn_rqst as the Loopback models. The modal form is initialized in the first step, followed by fetching a list from the backend using $scope.Se_chnl_find() to populate a dropdown menu within the modal.

After filling out the form, the submit function is called which triggers the creation of a request using Se_segn_rqst.create($scope.rqst) where $scope.rqst holds the necessary parameters for the request creation.

However, the issue arises when attempting to retrieve the ID of the latest created request and store it in a global variable. Even though the record is successfully created in the backend, the find function does not return any data. Curiously, testing the find filter in Strongloop/Loopback explorer yields results while calling it from the controller doesn't.

    codeApp.controller('ModalInstanceCtrl', function($scope, $modalInstance, $state, Se_chnl, Se_segn_rqst) {

    var defaultForm = {
        cmpgn_nm: "",
        cmpgn_id: "",
        strgy_id: "",
        rqst_typ_cd: "",
        chnl_id: ""
    }
    $scope.channels = Se_chnl.find({
        filter: {
            "fields": {
                "chnl_nm": true,
                "chnl_id": true
            }
        }
    });

    $scope.rqst = angular.copy(defaultForm);

    $scope.rqst.rqst_id = 0;

    $scope.submit = function(reqForm) {

        $scope.rqst.rqst_nm = $scope.rqst.cmpgn_nm;
        $scope.rqst.rqst_stat_cd = 'DRAFT';
        $scope.rqst.insrt_user_id = $scope.$parent.user_id;
        $scope.rqst.insrt_dt = new Date();

        Se_segn_rqst.create($scope.rqst);

        $scope.$parent.requested_id = Se_segn_rqst.find({
            filter: {
                "fields": {
                    "rqst_id": true
                },
                "order": "insrt_dt DESC",
                "limit": 1,
                "where": {
                    "rqst_stat_cd": "DRAFT",
                    "insrt_user_id": "xyz123"
                }
            }
        });

        $modalInstance.dismiss('cancel');

    };

    $scope.resetForm = function(reqForm) {
        $scope.rqst = angular.copy(defaultForm);
        reqForm.$setPristine();
        reqForm.$setUntouched();
    };
});

The key part that fails to return a value is highlighted above. My aim is to have an ID stored in the requested_id global variable after execution. While there seems to be no syntax error since the filter performs correctly in the Strongloop explorer, the discrepancy remains unresolved.

$scope.$parent.requested_id = Se_segn_rqst.find({
            filter: {
                "fields": {
                    "rqst_id": true
                },
                "order": "insrt_dt DESC",
                "limit": 1,
                "where": {
                    "rqst_stat_cd": "DRAFT",
                    "insrt_user_id": "xyz123"
                }
            }
        });

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

What is the best way to create a continuous loop in a command line node application?

Looking to develop a small node application that continuously prompts the user for input, processes it conditionally, and loops back until an exit command is given. Specifically, I aim to create or convert base 36 timestamps. The app would have three cond ...

Stop the occurrence of OpenCPU javascript error pop-up notifications

I'm currently experiencing an error related to CORs during a test deployment of OpenCPU. While I may create a separate question for this issue in the future, for now, I am wondering if it is possible for the deployment to fail without alerting the end ...

interact with the server in order to make changes to a document

I need to create a console program that can communicate with a server without requiring users to log in. The program should have the capability to write to a file on the server, such as incrementing a number in a text file every time it is run. For example ...

What mistake have I made in my JavaScript code?

Data Entry: let name = 'Sophia'; console.log('Hello, my name is', + name + '.'); Result: SyntaxError: Unexpected identifier ...

Ensure Angular Reactive Forms do not include empty fields when submitting the form

Is there a way to retrieve only the fields with values entered by the user from a form and exclude empty fields in the resulting JSON object? Currently, the JSON object still includes empty quotation marks for empty inputs. Thank you! Current: { "user ...

Combining multiple 'Eithers' and 'Promises' in fp-ts: A guide to piping and chaining operations

Recently, I began working with fp-ts and wanted to create a method with functional-like behavior that would: Parse a bearer token Verify the validity of the user using the parsed token import { Request } from 'express'; import { either } from & ...

Is there a way to delegate properties in Angular 2+ similar to React?

When working with React, I have found it convenient to pass props down dynamically using the spread operator: function SomeComponent(props) { const {takeOutProp, ...restOfProps} = props; return <div {...restOfProps}/>; } Now, I am curious how I ...

What could be the reason my spin animation is only triggered once upon button press?

Every time the submit button is clicked, I want all the images to spin. However, this rotation effect only works the first time the button is pressed, until the page is refreshed. I have used jQuery to add and remove a class from the images when the button ...

Accessing information from a JSON array

I am encountering difficulties in fetching data from a JSON file. Whenever I click the button, it triggers the call function which then retrieves data from the json.jsp file containing an array. However, I am unable to retrieve the data successfully. Below ...

Developing a real-time form that syncs with a MYSQL database for automatic updates

I am currently working on developing a form with multiple drop-down menus. The first dropdown is populated with 'Customer Name' data retrieved from my MYSQL database. Upon selection, the second dropdown menu below it should display the available ...

Angular UI.Router's $state templates use $rootScope for ng-models

I'm encountering a strange issue where the ui-view template in my $state controller seems to be interacting with $rootScope instead of $scope. However, it does initialize $scope variables properly. For instance: if I have $scope.name = "test" and i ...

Attempting to update state on a component that is no longer mounted

There are many instances in my components where I find myself needing to execute the following code: function handleFormSubmit() { this.setState({loading: true}) someAsyncFunction() .then(() => { return this.props.onSuccess() }) . ...

Determining outcomes of forms added in real-time

Need assistance with dynamically creating tickets, calculating prices, and displaying results when additional tickets are added. Currently facing an issue where price data is not being calculated when a new ticket is added. Any help would be greatly apprec ...

Organizing Files: Importing Sub-Components in Node.js

Here is my Node.js module/npm package structure: |- dist/ |- - requirejs/ |- - - [content in amd pattern ...] |- - node/ |- - - index.js |- - - submodules/ |- - - - submodule1.js |- - - - [submodule2.js and so on] |- package.json |- README.md I can easil ...

Tips for displaying a progress bar during the loading of controls

Is there a way to show a progress bar while dynamically generating controls based on the user selection from a dropdown list? The values for these controls are retrieved from a database. ...

Using variables in a Post request for a JSON object with JavaScript

I am attempting to retrieve an Xmlhttp.response from a website by using the following code snippet: var apiUrl = "http://somesite/someapicall/correctapiKey"; var xmlHttp = new XMLHttpRequest(); xmlHttp.open("POST", apiUrl, false); xmlHttp.setRequestHeader ...

Adding code to a React application significantly lowers the lighthouse performance score

After testing different factors affecting my low lighthouse score, I discovered that removing a specific script resulted in a 30% improvement. The script in question is for a web chat help app called Smartupp. Here is the code snippet: <script ...

Determining the clicked node in a JavaScript environment

There are multiple spans with labels. <span class="viewEdit">View and edit class one.</span> <span class="viewEdit">View and edit class two.</span> <span class="viewEdit">View and edit class three.</span> <span clas ...

Merge the contents of three arrays into a single array

I am seeking a more streamlined method to merge these three arrays: two data arrays and a "cross" array that connects them. 3 Arrays var drives = [ {"drivesId": "rwd", "name": "RWD"}, {"drivesId": "fwd", "name": "FWD"}, {" ...

What is the best practice for naming variables in JavaScript?

Currently, my API is built using PHP Laravel and MySQL, which uses snake_case for field names. I am considering using the same naming convention in client-side JavaScript to make it easier to transfer field names from PHP code to JavaScript code and when m ...