0
votes

i'm facing some problems when using an extended user model in my AngularJS application.

here is my user.json:

{
  "name": "user",
  "base": "User",
  "strict": false,
  "idInjection": true,
  "properties": {
    "clientType": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": []
}

here is my model-config.json:

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ]
  },
  "User": {
    "dataSource": "mongo"
  },
  "AccessToken": {
    "dataSource": "mongo",
    "public": false
  },
  "ACL": {
    "dataSource": "mongo",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "mongo",
    "public": false
  },
  "Role": {
    "dataSource": "mongo",
    "public": false
  },
  "Store": {
    "dataSource": "mongo",
    "public": true
  },
  "user": {
    "dataSource": "mongo",
    "public": true
  }
}

this is my UserCtrl.js

angular.module('app.controllers.user', [])

    .controller('UserCtrl', ['user', function (user) {

        var vm = this;

        vm.addUser = function () {
            user.create({
                firstName: vm.firstName,
                lastName: vm.lastName,
                email: vm.email,
                password: vm.password,
                userType: 'customer'
            })
                .$promise
                .then(function (c) {
                    console.log('added user: ' + c.email);
                });
        };
    }])

i'm getting the following error:

Error: [$injector:unpr] Unknown provider: userProvider <- user <- UserCtrl

if i use 'User' instead of 'user' it works, but it doesn't use my extended user-model with the specified ACL (READ for everyone)

i've read that you can specify var myUser = app.model.user to make sure that LoopBack uses the extended model. but i don't know how to do that in AngularJS since i specify the model as function parameter in the controller..

can you tell me how to use my extended user model within my AngularJS app?

thanks in advance!!

2

2 Answers

0
votes

Do you have your user model generated inside Angular client library? If your application works when you use loopback auto-generated "User" model, then my best guess is that you have created your extended model "user", after you initially generated your angular services. If you are not using grunt task then you should regenerate angular services to update file with all changes and new models that you added since you last time generated the file.

Use lb-ng command to do it. As documentation suggests

For example, if your application has the standard LoopBack project layout, then in the /client sub-directory, enter these commands:

$ mkdir js
$ lb-ng ../server/server.js js/lb-services.js

You can find more information on the following link http://docs.strongloop.com/display/public/LB/AngularJS+JavaScript+SDK

-3
votes

You need to define a Service, Factory, Provider, Value or Constant called 'user' in order for the service to be injectable in your controller. I do not see either of these in your post.

My suggestion is, if your extended user model is an instance of a class, then use a service:

app.service('user', fn);

If your extended user model is an object literal in JSON format, then use a factory:

app.factory('user', function() { return { ... }; });