How can I effectively transmit data using the AngularJs Sdk in the Loopback factory?

I am struggling with the following code snippet in my Angular factory:

getPermissions: function(modelId){

var Pers = Permission.find({
    filter : {
        where : {
            and : [
            {
                user_id : $rootScope.userId
            },
            {
                permission_id : modelId
            }
            ]
        }
    }
}, function(data){
    var Pers = data;
});

console.log(Pers);
},

When I check the console, I see:

 [$promise: c, $resolved: false]

Furthermore, when I try to use this code in my app function like so:

$scope.optionsCrud = getFoo.getPermissions(2);

console.log($scope.optionsCrud);

The console output is:

undefined

I am unable to iterate over the array. Does anyone have suggestions on how to fix this issue?

Answer №1

To retrieve the value Pers, you can use the following code:

console.log(Pers);
return Pers;

Full Code,

getPermissions: function(modelId){
    var Pers = Permission.find({
        filter : {
            where : {
                and : [
                {
                    user_id : $rootScope.userId
                },
                {
                    permission_id : modelId
                }
                ]
            }
        }
    }, function(data){
        var Pers = data;
    });
    console.log(Pers);
    return Pers;
},

Afterwards, to access your data, you can do the following:

$scope.optionsCrud = getFoo.getPermissions(2);
$scope.optionsCrud.$promise.then(function(data){
// or simply use $scope.optionsCrud.then if the previous line doesn't work
    console.log(data);
});

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

Utilize the nest function in D3 to organize flat data with a parent key into a hierarchical structure

I'm searching for an elegant and efficient solution to transform my input data into a hierarchical structure using d3.js nest operator. Here is an example of the input data: [ {id: 1, name: "Peter"}, {id: 2, name: "Paul", manager: 1}, {id: 3, name: " ...

Utilizing bcrypt in an axios request

Currently, I am working on an axios call to a specific json file. My goal is to obtain input from a front-end framework and then pass that data to my server using express. The task at hand involves encrypting the password retrieved from request.body.passw ...

Accessing template attribute value in AngularJS controllerIn AngularJS, retrieving the

I am new to using angularjs version 1.6.4 and I have implemented the module leon/angular-upload for file upload functionality in minify version. After a successful upload, the server returns a JSON object containing information about the uploaded file in t ...

Picture goes missing from slideshow

I am currently using a CSS + Javascript slideshow on my website, inspired by an example from the W3Schools website. Here is the code I have adapted for my web application: $(document).ready(function() { var slideIndex = 1; function showSlides(n) { ...

The next column featuring a dropdown menu

My goal is to make this sketch operational. The red elements in the sketch need to be programmed. Currently, I am using angularJS and bootstrap with the following code: <div class="col-md-6"> <div class="form-group"> <label&g ...

Send the user back to the homepage without the need to refresh the page

When the user clicks "Okay" in my window.prompt, the code below opens a new tab. Is there a way to direct the user to the home page without opening a new tab? if (window.confirm("Do you really want to leave?")) { window.open("./Tutoria ...

Steps for creating an AJAX request to a variable defined within the local scope

I want to create a list using a JSON object that I already have stored in a variable. I have been exploring the dynatable library and its documentation on populating a table using AJAX to receive JSON data. However, I am stuck on how to make it work with ...

Ways to choose the correct css styling

I'm currently working on creating navigation using Bootstrap 4, but I've hit a roadblock in adjusting the li items to suit my navigation needs. I'm struggling to figure out how to select these li tags since they are nested in multiple classe ...

Combine multiple arrays in JavaScript into a single array

Here is the array I am working with: array = ['bla', ['ble', 'bli'], 'blo', ['blu']] I need to transform it into this format: array = ['bla', 'ble', 'bli', 'blo', &a ...

Developing HTML components for the purpose of testing JavaScript

I am encountering a specific issue in my react component where I am using the following commands: document.getElementById('modalsContainer').appendChild(recognitionProfileZoom); document.getElementById('modalsContainer').appendChild(ca ...

Generating a dynamic sitemap with Next.js

I have been working on a project where I need to create a sitemap, and I am currently using next-sitemap for generation. However, I've encountered an issue with this method, as well as other solutions I've come across, because the sitemap is only ...

utilizing the dropdown label for validating the value of another dropdown

I'm facing a challenge with two dropdowns: <select name="first_value" id="first_value"> <option value="<?php print"$sum_jan" ?>">January</option> <option value="<?php print"$sum_feb" ?>">February</option ...

The custom classes I have created in Material UI are being overshadowed by the default classes provided by Material UI

I am trying to change the color of a TextField label to black when it is focused. I used classes in InputProps with a variable, but unfortunately, the default styling of material UI is taking precedence in chrome dev tools. Below is the code snippet. Pleas ...

Finding the value of a selected checkbox in a Bootstrap multi-select checkbox dropdown

When using a bootstarp multi select checkbox dropdown to select multiple values, I am able to see the list of selected values when checking or unchecking. However, I am wondering how can I specifically get the value of the clicked item. For instance: If I ...

How can I include a nested object in an ng-repeat loop in Angular?

I'm new to using Angular so please excuse my basic question. Here is the structure of my model for posts and comments: [ { "title": "awesome post", "desc": "asdf", "comment_set": [ { "id": 2, ...

How can I toggle a clicked switch in React MUI instead of all switches?

This is the current state in my parent component: const [feedbackType, setFeedbackType] = useState({ manual: true, auto: false, }); I am passing this state as a prop to the child component. In the child component, I have the following code: co ...

Does Javacc have a capability to generate JavaScript code as an output?

Is there a parser generator available that can take a Javacc grammar file (.jj) and produce a JavaScript parser instead of Java? If not, what would be involved in converting the .jj file into a format that ANTLR can interpret (since it has the capability ...

I am interested in developing a program using Javascript or JQuery that can effectively capture the anchor text within <a></a> link tags

I want to automatically capture the link on my website and create a system where, when users click on a specific link, it opens the captured link in a new tab without requiring browser permission and replaces the current tab with a different link. For exa ...

Creating a dynamic mongoDB query string for field selection in a JavaScript application: A step-by-step guide

I am currently working on a Node.js application that utilizes MongoDB. The schema for my customers collection is as follows: { '_id' : 1234341264876876143 , 'profile':{ 'name': 'bob', &ap ...

Initializing Access Model in Sails.js Service启

The inquiry: In the context of sails.js, it appears that Services are initialized before Models during the initialization process. Is there a way to alter this sequence? Can Models be loaded before Services? If not, then how can one retrieve specific se ...