Search code examples
javascriptnode.jsadminloopbackjsroles

How to create a Role in Loopback/Node.Js


I created this method to create an Admin Role. I have to check in user table if the admin field is set to 0 or 1 of the current user to detect if it's a normal user or an admin. Problem is when I console.log('obj',objUser) I get this :

obj Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }

This is what I've built so far :

Role.registerResolver('Admin', function(role, context, cb) {

            //Q: Is the user logged in? (there will be an accessToken with an ID if so)
            var userId = context.accessToken.userId;

            if (!userId) {
              //A: No, user is NOT logged in: callback with FALSE
              return process.nextTick(() => cb(null, false));
            }

            var user = app.models.user;

            var objUser = user.findById(userId);
            console.log('obj',objUser)
            if(objUser.admin==1){
              return cb(null, true);
            }
            else{
              return cb(null, false);
            }

            });

Do you have any idea what's wrong am I doing. Any help would be appreciated. Thank you.


Solution

  • you should read about javascript Promises, it's crucial to understand javascript in general and async programming. Also, check the async/await