1

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


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);
        }
    }
4

1 回答 1

4

在使用 API 调用之前,请务必查看 API 文档。这是 write 的文档

它说:

参数:

值 - 强制。细绳。要加密的数据。当设置为 null 时,密钥被删除。

改变:

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

至:

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

您只能使用该 API 存储字符串。如果要存储对象,则必须使用JSON.stringify(对象到字符串)和JSON.parse(字符串到对象)。如果你想从 string 到 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

于 2014-06-26T18:35:36.903 回答