Javascript Worklight 6.1加密缓存错误

Javascript Worklight 6.1加密缓存错误,javascript,ibm-mobilefirst,worklight-security,Javascript,Ibm Mobilefirst,Worklight Security,我正在编写一个代码,将一些信息存储到worklight加密缓存中。 我试图加密一个值,它是本地数据库中的主键,看起来像50005。这是一个数字,我将它传递给加密缓存的write方法 我正在web预览环境中运行项目。 错误为无效的参数值“50005”,应为null或“string”。 下面是代码片段 function setUserId(userId){ WL.EncryptedCache.write("USER_ID",userId, onCompleteHandler, onError

我正在编写一个代码,将一些信息存储到worklight加密缓存中。 我试图加密一个值,它是本地数据库中的主键,看起来像50005。这是一个数字,我将它传递给加密缓存的write方法 我正在web预览环境中运行项目。 错误为无效的参数值“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);
        }
    }

在使用API调用之前,请务必查看API文档

它说:

参数:

值-强制。一串要加密的数据。设置为null时,将删除密钥

更改:

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

只能使用该API存储字符串。如果要存储对象,必须使用“对象到字符串”和“字符串到对象”。如果要从字符串转换为int,可以使用如下函数:parseIntuserId

或者,您也可以使用JSONStoreAPI。注意,它仅在Worklight v6.2的Android和iOS上受支持,在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的文档。

在使用API调用之前,请务必查看API文档

它说:

参数:

值-强制。一串要加密的数据。设置为null时,将删除密钥

更改:

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

只能使用该API存储字符串。如果要存储对象,必须使用“对象到字符串”和“字符串到对象”。如果要从字符串转换为int,可以使用如下函数:parseIntuserId

或者,您也可以使用JSONStoreAPI。注意,它仅在Worklight v6.2的Android和iOS上受支持,在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的文档