我正在编写代码以将一些信息存储到 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 文档。 Here's the documentation for write .
它说:
Parameters:
value - Mandatory. String. The data to encrypt. When set to null, the key is removed.
改变:
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
});
有文档 here用于 JSONStore。
关于javascript - Worklight 6.1 加密缓存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24436789/