0

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!!

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
uNki
  • 193
  • 2
  • 13

2 Answers2

0

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

A.Z.
  • 1,638
  • 2
  • 18
  • 27
  • thank you. i've done that all the time. i think the problem is, that the AngularJS SDK from LoopBack always injects the models with capital first letter. if i create a model named 'customer' it have to inject it with 'Customer' into the controller function to use it. the existing base model 'User' is already in that format. so my extended user model 'user' can't be injected with 'User' because thats already the name of the base model. there must be a way to distinguish between them – uNki Apr 05 '15 at 21:18
  • I am not sure about that but if that is the case then just rename your "user" model to something else e.g. CustomUser. It's not good practice anyway to have classes/models distinguished only by capital letter. – A.Z. Apr 05 '15 at 21:45
  • many examples in the net just say "hey, just extend the User model with your custom model and name it 'user'" .. that's even stated in the LoopBack docs. the whole api is built on top of that name. so if i use 'CustomUser' i'll have to "live with it" in my app. but i just want to use "user". and finally: there has to be a solution for that :) – uNki Apr 05 '15 at 21:58
  • 2
    @uNki, I occur encounter this problem same with you, how did you fix this? – laifjei Jun 25 '15 at 15:58
-3

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 { ... }; });
  • thank you. is your answer LoopBack-related? i really don't know how to interpret your answer in the LoopBack context. if i inject 'User' in the controller function it works, because LoopBack makes that possible out of the box. it just doesn't work with my extended User model 'user' – uNki Apr 05 '15 at 14:49
  • My answer is angularjs related. The error message is telling you that it cannot find the service to inject into your controller. – user3890194 Apr 05 '15 at 14:53
  • yes i understand that, but i need a solution that works with LoopBack because this is where all the business logic and crud-stuff happens. i don't need to write services etc. to communicate with the backend. that's what LoopBack does. There must be a simple solution to use an extended User model – uNki Apr 05 '15 at 14:54