strongloop / loopback-example-access-control

An example demonstrating LoopBack access control mechanisms.
Other
370 stars 168 forks source link

user instance is not valid #84

Closed the1mills closed 8 years ago

the1mills commented 8 years ago

Followed the instructions in the readme, got this error:

/Users/t_millal/WebstormProjects/crucible-poc/server/boot/script.js:29
      throw err;
      ^
ValidationError: The `user` instance is not valid. Details: `email` is invalid (value: "john@doe.com"); `username` is invalid (value: "John").,ValidationError: The `user` instance is not valid. Details: `email` is invalid (value: "jane@doe.com"); `username` is invalid (value: "Jane").,ValidationError: The `user` instance is not valid. Details: `email` is invalid (value: "bob@projects.com"); `username` is invalid (value: "Bob").

anyone know what might cause this? thanks

ORESoftware commented 8 years ago

I fixed this problem - what was happening is that the automigration was not complete before creating the db entities.

For your use, the following is the same script in the readme, but blocks on automigration:


// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: loopback-example-access-control
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0

const async = require('async');

module.exports = function (app, cb) {

  var User = app.models.user;
  var Role = app.models.Role;
  var Project = app.models.project;
  var RoleMapping = app.models.RoleMapping;
  var Team = app.models.team;

  function automigrateItem(item) {
    return function (cb) {
      app.dataSources.psql.automigrate(item, function (err) {
        err ? cb(err) : (function () {
          console.log('automigration is done for the following entity => ' + item);
          cb(null);
        })();
      });
    }
  }

  async.parallel([
      automigrateItem('user'),
      automigrateItem('project'),
      automigrateItem('team'),
      automigrateItem('Role'),
      automigrateItem('RoleMapping')
    ],

    function (err) {

      if (err) {
        cb(err);
      }
      else {

        User.create([
            {username: 'John', email: 'john@doe.com', password: 'opensesame'},
            {username: 'Jane', email: 'jane@doe.com', password: 'opensesame'},
            {username: 'Bob', email: 'bob@projects.com', password: 'opensesame'}
          ],

          function (err, users) {

            if (err) return cb(err);

            console.log('Created users:', users);

            async.parallel([
              function (cb) {
                // create project 1 and make john the owner
                users[0].projects.create({

                  name: 'project1',
                  balance: 100
                }, function (err, project) {

                  if (err) return cb(err);

                  console.log('Created project:', project);

                  // add team members
                  Team.create([
                    {ownerId: project.ownerId, memberId: users[0].id},
                    {ownerId: project.ownerId, memberId: users[1].id}
                  ], function (err, team) {
                    if (err) return cb(err);
                    console.log('Created team:', team);
                    cb(null);
                  });
                });
              },
              function (cb) {
                //create project 2 and make jane the owner
                users[1].projects.create({

                  name: 'project2',
                  balance: 100

                }, function (err, project) {

                  if (err) return cb(err);

                  console.log('Created project:', project);

                  //add team members
                  Team.create({

                    ownerId: project.ownerId,
                    memberId: users[1].id

                  }, function (err, team) {

                    if (err) return cb(err);
                    console.log('Created team:', team);
                    cb(null);
                  });
                });
              }

            ], function (err) {

              if (err) return cb(err);

              //create the admin role
              Role.create({

                name: 'admin'

              }, function (err, role) {

                if (err) return cb(err);

                console.log('Created role:', role);

                //make bob an admin
                role.principals.create({
                  principalType: RoleMapping.USER,
                  principalId: users[2].id
                }, function (err, principal) {

                  if (err) return cb(err);

                  console.log('Created principal:', principal);

                  cb(null);

                });
              });
            });

          });
      }
    });

};
ORESoftware commented 8 years ago

the above assumes you are using postgres (psql)

this line above

app.dataSources.psql.automigrate(item, function (err) {})

will differ for whoever you are

ORESoftware commented 8 years ago

(you can close issue, thx)

Nopzen commented 6 years ago

@ORESoftware - This reply saved me a massive headache! Thanks!

the1mills commented 6 years ago

Np! This automigration stuff is massively useful for working with standard RDBMs. (I am ORESoftware's alter ego :)