Worklight 6.1加密缓存错误

时间:2014-06-26 17:30:25

标签: javascript ibm-mobilefirst worklight-security

我正在编写一个代码来存储一些信息到worklight加密缓存 我正在尝试加密一个值,这个值是我本地数据库中的主键,看起来像 50005 它是一个数字,我将它传递给加密缓存的方法
我在网络预览环境中运行项目 错误是无效的参数值'50005',预期为null或'string'。
以下是代码段

function setUserId(userId){
    WL.EncryptedCache.write("USER_ID",userId, onCompleteHandler, onErrorHandler);
    }

function onCompleteHandler(status){
        console.log("Global cache write success.");
    }

    function onErrorHandler(status){
        console.log("Global cache open error."+status);
        switch(status){
        case WL.EncryptedCache.ERROR_KEY_CREATION_IN_PROGRESS:
            console.log("ERROR: KEY CREATION IN PROGRESS");
            break;
        case WL.EncryptedCache.ERROR_LOCAL_STORAGE_NOT_SUPPORTED:
            console.log("ERROR: LOCAL STORAGE NOT SUPPORTED");
            break;
        case WL.EncryptedCache.ERROR_NO_EOC:
            console.log("ERROR: NO EOC");
            break;
        case WL.EncryptedCache.ERROR_COULD_NOT_GENERATE_KEY:
            console.log("ERROR: COULD NOT GENERATE KEY");
            break;
        case WL.EncryptedCache.ERROR_CREDENTIALS_MISMATCH:
            console.log("ERROR: CREDENTIALS MISMATCH");
            break;
        default:
            console.log("AN ERROR HAS OCCURED. STATUS :: " + status);
        }
    }

1 个答案:

答案 0 :(得分:4)

在使用API​​调用之前,请始终查看API文档。 Here's the documentation for write

它说:

  

参数:

     

价值 - 强制性。串。要加密的数据。设置为null时,键将被删除。

变化:

WL.EncryptedCache.write("USER_ID",userId, onCompleteHandler, onErrorHandler);

为:

WL.EncryptedCache.write("USER_ID",userId.toString(), onCompleteHandler, onErrorHandler);

您只能使用该API存储字符串。如果要存储对象,则必须使用JSON.stringify(对象到字符串)和JSON.parse(字符串到对象)。如果您想从字符串转到int,可以使用parseInt函数,如下所示:parseInt(userId)

或者,您可以使用JSONStore API。请注意,它仅在Android和iOS上受支持(在Worklight v6.2中,它也支持WP8和W8)。代码看起来像这样:

var collections = {
  users : {
    searchFields : {'userid' : 'integer'}
  }
};

var options = {
  password: '123'
};

WL.JSONStore.init(collections, options)
.then(function () {
  return WL.JSONStore.get('users').add({userid: 50005});
})
.then(function () {
  return WL.JSONStore.get('users').findAll();//or .find({userid: 50005})
})
.then(function (results) {
  WL.Logger.debug(results);
})
.fail(function () {
  //handle failure in any of the API calls above
});

JSONStore有文档here

相关问题