3

I'm new to loopback, however I followed the steps to install and scaffold my folder (loopback-server), inside server/boot/ I created one file script.js and included the following code:

    module.exports = function(app) {
var MongoDB = app.dataSources.MongoDB;

MongoDB.automigrate('Customer', function(err) {
   if (err) throw (err);
   var Customer = app.models.Customer;

   Customer.create([
    {username: 'admin', email: 'admin@admin.com', password: 'abcdef'},
    {username: 'user', email: 'muppala@ust.hk', password: 'abcdef'}
  ], function(err, users) {
    if (err) throw (err);
     var Role = app.models.Role;
    var RoleMapping = app.models.RoleMapping;

    //create the admin role
    Role.create({
      name: 'admin'
    }, function(err, role) {
      if (err) throw (err);
       //make admin
      role.principals.create({
        principalType: RoleMapping.USER,
        principalId: users[0].id
      }, function(err, principal) {
        if (err) throw (err);
      });
    });
  });
});

};

Now I'm getting this error:

terminal error

I commented this file out and didn't get that error. By the way, I tried to change the keys and values of {username: 'admin',..} and Role.create({name: 'admin'},.... but either doesn't work or it works but I can't login as admin.

halfer
  • 19,824
  • 17
  • 99
  • 186
shireef khatab
  • 977
  • 2
  • 13
  • 33

1 Answers1

4

If you're Role entity is being stored in a database then this code would try to create that Role entity (with a name of "admin") each time your application starts. However, after the first time, that Role would already exist, thus you get an error that you have a duplicate "name". What you might want to do is check that the Role does not already exist, or not store the Roles in your DB.

You could add some code to check the current DB and only add that Role if it doesn't exist. Something like this:

Role.find({ name: 'admin' }, function(err, results) {
    if (err) { /* handle this! */ }

    if (results.length < 1) {
        // now we know the DB doesn't have it already, so do the Role creation...
    }
});

Note that you would also want to check if that Role table already has the principals you're adding and only add them if they aren't already there.

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
  • Thank you, What you are saying makes perfect sense and now i cant see the error but .. when i login as admin and use the token for GET request it gives me this error: "error": { "name": "Error", "status": 401, "message": "Authorization Required", "statusCode": 401, "code": "AUTHORIZATION_REQUIRED", – shireef khatab Sep 15 '16 at 19:51
  • and please can u exlplain this more (If you're Role entity is being stored in a database then this code would try to create that Role entity (with a name of "admin") each time your application starts.) by the way i had an empty db before starting thr server – shireef khatab Sep 15 '16 at 19:59
  • The Authorization error completely depends on what you were trying to do and what ACL's you have in place on your models. As for the explanation of my text, I'm not sure how else to say that... if your `Role`s are being stored in a database, then you can't recreate them each time the application starts (which is what a boot script does). – Jordan Kasper Sep 15 '16 at 20:08
  • clear enough, but what i said about emptying my data before start the server , does it indicates that the role s are not being stored in database? sorry if that sound silly to u. – shireef khatab Sep 15 '16 at 20:51
  • I'm sorry, but I'm just not sure. It could be? Look at your `model-config.json` file and see what datasource the `Role` entity is set to - that should indicate if the Role is being stored or not. – Jordan Kasper Sep 16 '16 at 12:08
  • Thanks a lot, I have two more questions about loopback please have a look at then here http://stackoverflow.com/questions/39531658/loopback-model-settings-validateupsert-was-overriden-to-false and here http://stackoverflow.com/questions/39528371/loopback-error-authorization-required thanks again – shireef khatab Sep 16 '16 at 12:37