如何在 logic.js 文件中的 if 语句中检查 CTO 文件中的枚举
How to check enums from CTO file in an if statement in logic.js file
如果我在 .cto 文件中有一个简单的枚举,例如:
enum STATUS {
o ACTIVE
o INACTIVE
}
还有一个用户
participant User identified by name{
o String name
o STATUS status
}
如何检查函数中的状态,例如:
transaction isActive {
o User user
}
在 logic.js 文件中,我会有这样的代码:
return getParicipantRegistry(NS + '.User'){
.then(function(userRegistry) {
var u = userRegistry.get(user.name)
if (u.isActive == 'ACTIVE')
//some code
})
我的情况有点复杂(枚举有 6 种类型),但我试图简化它所以逻辑是相同的。
试试这个。
将交易中的用户更改为关系
transaction isActive{
-->User user
}
现在logic.js你可以得到用户的状态如
/**
* checkStatus
* @param {org.test.isActive} checkStatus // use your namespace instead of org.test
* @transaction
*/
function checkStatus(txParams){
// txParams are the parameters given when the transaction is submitted
// you can access user details by txParams.user
if(txParams.user.status == "ACTIVE"){
// do something
}
// then you can update the user by
return getParticipantRegistry(NS+'.User').then(function(userRegistry){
// or you can also check status directly here and do something
return userRegistry.update(txParams.user)
})
}
如果我在 .cto 文件中有一个简单的枚举,例如:
enum STATUS {
o ACTIVE
o INACTIVE
}
还有一个用户
participant User identified by name{
o String name
o STATUS status
}
如何检查函数中的状态,例如:
transaction isActive {
o User user
}
在 logic.js 文件中,我会有这样的代码:
return getParicipantRegistry(NS + '.User'){
.then(function(userRegistry) {
var u = userRegistry.get(user.name)
if (u.isActive == 'ACTIVE')
//some code
})
我的情况有点复杂(枚举有 6 种类型),但我试图简化它所以逻辑是相同的。
试试这个。
将交易中的用户更改为关系
transaction isActive{
-->User user
}
现在logic.js你可以得到用户的状态如
/**
* checkStatus
* @param {org.test.isActive} checkStatus // use your namespace instead of org.test
* @transaction
*/
function checkStatus(txParams){
// txParams are the parameters given when the transaction is submitted
// you can access user details by txParams.user
if(txParams.user.status == "ACTIVE"){
// do something
}
// then you can update the user by
return getParticipantRegistry(NS+'.User').then(function(userRegistry){
// or you can also check status directly here and do something
return userRegistry.update(txParams.user)
})
}