How to retrieve the latest created record using loopback api and angularjs?

1,476 views
Skip to first unread message

Amit Shah

unread,
Aug 20, 2015, 4:06:45 PM8/20/15
to LoopbackJS

Below is my Modal controller. Se_chnl and Se_segn_rqst are the Loopback models. I'm initializing the modal form in the first step. The $scope.Se_chnl_find() is getting me a list from the backend which I load as a dropdown menu in the modal. This call to loopback works fine.

Then later on once the form is filled, I call the submit function and in that I call the create function of loopback Se_segn_rqst.create($scope.rqst) $scope.rqst contains the parameters for creating that rqst.

Now, once I have created this "rqst", I want to retrieve ID of the latest created request by that user and store it in the global variable. But the loopback api/MySQL doesn't return anything. Record is created in the backend when create is used. But the find function doesn't work.

I tried the find filter in Strongloop/Loopback explorer and it works there. Not sure why it is not returning anything when I tried it from the controller.

    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();
    };
});

This is the piece returning no value. I want an id in the requested_id global variable. The filter is performing correctly in the Strongloop explorer, so there is no syntax error.

$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"
                }
            }
        });

Simon Ho

unread,
Aug 20, 2015, 5:18:05 PM8/20/15
to LoopbackJS
What is the name of your LoopBack model? Is there a `rqst_id` field or is the primary key `id`?

Amit Shah

unread,
Aug 20, 2015, 5:37:56 PM8/20/15
to LoopbackJS
The rqst_id field is the primary key.

I did some more research and found that the model had "_selectable" property for rqst_id field set to false.
On changing the _selectable property to true, I got back a value.
But the value I got was after the whole modal controller was executed.
The create doesn't happen instantaneously.
So when the create function is called, there is nothing written to the backend instantaneously.

Simon Ho

unread,
Aug 20, 2015, 5:40:17 PM8/20/15
to LoopbackJS
Are you using the Angular SDK? Can you try a basic find operation without the rest of your code? Can you provide a test project on GitHub for me to reproduce the issue?

Amit Shah

unread,
Aug 20, 2015, 5:41:20 PM8/20/15
to LoopbackJS
Here is the model

{
  "name": "se_segn_rqst",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "schema": "segtool",
    "table": "se_segn_rqst"
  },
  "properties": {
    "rqst_id": {
      "type": "Number",
      "id": true,
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "RQST_ID",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "rqst_nm": {
      "type": "String",
      "required": false,
      "length": 50,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "RQST_NM",
        "dataType": "varchar",
        "dataLength": 50,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "rqst_stat_cd": {
      "type": "String",
      "required": false,
      "length": 50,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "RQST_STAT_CD",
        "dataType": "varchar",
        "dataLength": 50,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "chnl_id": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "CHNL_ID",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "strgy_id": {
      "type": "String",
      "required": false,
      "length": 50,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "STRGY_ID",
        "dataType": "varchar",
        "dataLength": 50,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "cmpgn_id": {
      "type": "String",
      "required": false,
      "length": 30,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "CMPGN_ID",
        "dataType": "varchar",
        "dataLength": 30,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "cmpgn_nm": {
      "type": "String",
      "required": false,
      "length": 50,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "CMPGN_NM",
        "dataType": "varchar",
        "dataLength": 50,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "prvcy_mail_ind": {
      "type": "Boolean",
      "required": false,
      "length": 1,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "PRVCY_MAIL_IND",
        "dataType": "char",
        "dataLength": 1,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "prvcy_phone_ind": {
      "type": "Boolean",
      "required": false,
      "length": 1,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "PRVCY_PHONE_IND",
        "dataType": "char",
        "dataLength": 1,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "prvcy_fcra_ind": {
      "type": "Boolean",
      "required": false,
      "length": 1,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "PRVCY_FCRA_IND",
        "dataType": "char",
        "dataLength": 1,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "prvcy_glb_ind": {
      "type": "Boolean",
      "required": false,
      "length": 1,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "PRVCY_GLB_IND",
        "dataType": "char",
        "dataLength": 1,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "prvcy_piped_ind": {
      "type": "Boolean",
      "required": false,
      "length": 1,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "PRVCY_PIPED_IND",
        "dataType": "char",
        "dataLength": 1,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "prvcy_facta_ind": {
      "type": "Boolean",
      "required": false,
      "length": 1,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "PRVCY_FACTA_IND",
        "dataType": "char",
        "dataLength": 1,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "prvcy_swpstk_ind": {
      "type": "Boolean",
      "required": false,
      "length": 1,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "PRVCY_SWPSTK_IND",
        "dataType": "char",
        "dataLength": 1,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "prev_rqst_id": {
      "type": "String",
      "required": false,
      "length": 30,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "PREV_RQST_ID",
        "dataType": "varchar",
        "dataLength": 30,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "rqst_typ_cd": {
      "type": "String",
      "required": false,
      "length": 30,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "RQST_TYP_CD",
        "dataType": "varchar",
        "dataLength": 30,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "insrt_dt": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "INSRT_DT",
        "dataType": "date",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "insrt_user_id": {
      "type": "String",
      "required": false,
      "length": 10,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "INSRT_USER_ID",
        "dataType": "varchar",
        "dataLength": 10,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "upd_dt": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "UPD_DT",
        "dataType": "date",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "upd_user_id": {
      "type": "String",
      "required": false,
      "length": 10,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "UPD_USER_ID",
        "dataType": "varchar",
        "dataLength": 10,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "prvcy_email_ind": {
      "type": "String",
      "required": false,
      "length": 30,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "PRVCY_EMAIL_IND",
        "dataType": "varchar",
        "dataLength": 30,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    }
  },
  "validations": [],
  "relations": {
    "se_segn_rqst_popn": {
      "type": "hasMany",
      "model": "se_segn_rqst_popn",
      "foreignKey": "rqst_id"
    },
    "se_segn_rqst_excln": {
      "type": "hasMany",
      "model": "se_segn_rqst_excln",
      "foreignKey": "rqst_id"
    },
    "se_chnl": {
      "type": "belongsTo",
      "model": "se_chnl",
      "foreignKey": "chnl_id"
    }
  },
  "acls": [],
  "methods": []
}



Simon Ho

unread,
Aug 20, 2015, 6:01:48 PM8/20/15
to LoopbackJS
I don't see any issues with your query. It could be a bug in Angular SDK, but I need to confirm. I'm also checking with another dev here to make sure that is the case.
...

Simon Ho

unread,
Aug 20, 2015, 6:08:23 PM8/20/15
to LoopbackJS
Can you test a basic find or findOne operation in isolation (without filters, etc) to see if you get the id field back?

Amit Shah

unread,
Aug 20, 2015, 6:13:23 PM8/20/15
to LoopbackJS

Simon Ho

unread,
Aug 20, 2015, 6:19:20 PM8/20/15
to LoopbackJS
Did you try it in the same fashion as the example provided in loopback-example-angular? I notice you are not using promises either. See https://github.com/strongloop/loopback-example-angular

Amit Shah

unread,
Aug 21, 2015, 11:32:40 AM8/21/15
to LoopbackJS
Hey Simon,
Thanks for your prompt responses.
I'm new to loopback and angular... 
Looking into the "promise" property. Will update this thread soon with what I find.

Is there any video tutorial for using promises?

Simon Ho

unread,
Aug 24, 2015, 4:51:32 AM8/24/15
to LoopbackJS
Hi Amit,

Have you had the chance to try out the methods in my example? As for video tutorials for using promises, there are tons on YouTube or you can try https://egghead.io/. They have a lot of good angular videos, but some of them are not free.

Amit Shah

unread,
Aug 27, 2015, 11:23:22 AM8/27/15
to LoopbackJS
Hey Simon,

Yes, used promise and also put the state change inside of the promise.
It worked. 

Thanks a lot.

Simon Ho

unread,
Aug 27, 2015, 11:44:34 AM8/27/15
to LoopbackJS
NP, glad you got it working.
Reply all
Reply to author
Forward
0 new messages