3

I'm getting expected resource or concept errors with two functions. I am creating the Concept and Resource and assigning their values the correct way, according to the hyperledger composer API example.

What my code does:

createUser()
User contains a Concept called UserData that consists of four String fields. I try to create a new user by first creating a new UserData Concept and updating its fields, then setting the User's userData to this new Concept.

grantAccess()
Search for a user in the registry by ID and set its access value to true.

Both functions throw Error: Error trying invoke business network. Error: No valid responses from any peers. Response from attempted peer comms was an error: Error: chaincode error (status: 500, message: Error: Expected a Resource or Concept.)

.cto:

namespace org.acme.biznet

concept UserData {
    o String name
    o String id
    o String postcode
    o String birthdate
}

participant User identified by userId {
    o String userId
    o String name
    o UserData userData
    o Boolean access
}

transaction createUser {
  o UserData newUserData
}

transaction grantAccess {
    o String userId
}

event NewUserCreated {
    o User user
}

event UserAccessRightsChanged {
    o User user
    o Boolean oldValue
    o Boolean newValue
}

.js:

/**
* Creates a new user.
* @param {org.acme.biznet.createUser} createUser The create user transaction.
* @transaction
*/    
function createUser(createUser) {

    // create new instance of a User
    var factory = getFactory();
    var newUser = factory.newResource('org.acme.biznet', 'User', '123');
    var newData = factory.newConcept('org.acme.biznet', 'UserData');

    newData.name = createUser.newUserData.name;
    newData.id = createUser.newUserData.id;
    newData.postcode = createUser.newUserData.postcode;
    newData.birthdate = createUser.newUserData.birthdate;

    newUser.userData = newData;

    return getParticipantRegistry('org.acme.biznet.User')
    .then(function (userRegistry) {
        return userRegistry.update(createUser.user);
    })
    .then(function () {
        // Emit an event for the new user creation.
        var event = getFactory().newEvent('org.acme.biznet', 'NewUserCreated');
        event.user = newUser;
        emit(event);
    });
}

/**
* Grants access to the user data.
* @param {org.acme.biznet.grantAccess} userGrantAccess The grantAccess transaction.
* @transaction
*/
function grantAccess(userGrantAccess) {

    var existingAccessValue;
    var theUser;

    // Get the user from the registry given the id
    getParticipantRegistry('org.acme.biznet')
        .then(function (participantRegistry) {
            // Get the specific User from the participant registry.
            return participantRegistry.get(userGrantAccess.userId);
        })
        .then(function (user) {
            theUser = user;
            existingAccessValue = user.access;
            user.access = true;
        });

    // Get and update the user registry
    return getParticipantRegistry('org.acme.biznet.User')
        .then(function (userRegistry) {
            return userRegistry.update(theUser);
        })
        .then(function () {
            // Emit an event for the modified user rights.
            var event = getFactory().newEvent('org.acme.biznet', 'UserAccessRightsChanged');
            event.user = theUser;
            event.oldValue = existingAccessValue;
            event.newValue = theUser.access;
            emit(event);
        });
}
Joel
  • 51
  • 5

1 Answers1

0

This simplified function (based on the Bond Sample network) works in Composer Playground - with the modified Model.

/**
 * Creates a New User
 * @param {org.acme.biznet.CreateUser} cUser 
 * @transaction
 */
function createUser(cUser) {
console.log(cUser)
    return getParticipantRegistry('org.acme.biznet.User')
        .then(function (registry) {
            var factory = getFactory();
            // Create the user
            var user = factory.newResource('org.acme.biznet', 'User', cUser.newuserId);
            user.name = cUser.newname;
            user.userData = cUser.newuserData;
            user.access = cUser.newaccess;
            // Add the bond asset to the registry.
            return registry.add(user);
        });
}

Modified Model:

namespace org.acme.biznet

concept UserData {
    o String name
    o String id
    o String postcode
    o String birthdate
}

participant User identified by userId {
    o String userId
    o String name
    o UserData userData
    o Boolean access
}

transaction CreateUser {
    o String newuserId
    o String newname
    o UserData newuserData
    o Boolean newaccess
}

transaction grantAccess {
    o String userId
}

event NewUserCreated {
    o User user
}

event UserAccessRightsChanged {
    o User user
    o Boolean oldValue
    o Boolean newValue
}
R Thatcher
  • 5,550
  • 1
  • 7
  • 15