Ibm mobilefirst 嵌套的WLJSONStore调用未按预期顺序执行,也未向集合中添加项
这是带有dojo的Worklight 6.1代码,使用Chrome和std dev服务器Liberty进行测试。我想让这段代码做的是查询一个集合,该集合应该有0个或1个条目,并检索一个条目(如果存在),或者创建一个具有提供的一组值的条目。我要做的是为服务存储url、id和密码。如果这是安装后应用程序第一次运行,我想提示用户输入此信息并将其存储。稍后将添加提示用户的代码。如果不是应用程序的第一次运行,则应将值存储在集合中并检索。稍后我将添加代码,以允许用户更改和更新值 现在发生的事情是,.add似乎从未执行过,而且我通过设置的断点看到的执行顺序似乎很奇怪 这是设置代码Ibm mobilefirst 嵌套的WLJSONStore调用未按预期顺序执行,也未向集合中添加项,ibm-mobilefirst,jsonstore,Ibm Mobilefirst,Jsonstore,这是带有dojo的Worklight 6.1代码,使用Chrome和std dev服务器Liberty进行测试。我想让这段代码做的是查询一个集合,该集合应该有0个或1个条目,并检索一个条目(如果存在),或者创建一个具有提供的一组值的条目。我要做的是为服务存储url、id和密码。如果这是安装后应用程序第一次运行,我想提示用户输入此信息并将其存储。稍后将添加提示用户的代码。如果不是应用程序的第一次运行,则应将值存储在集合中并检索。稍后我将添加代码,以允许用户更改和更新值 现在发生的事情是,.add似
// set up the jsonStore
var collectionName = 'servers';
var collections = {};
collections[collectionName] = {};
// initialize the default jsonStore Monitor credentials
var jsonURL = 'http://myserver.com:9082';
var jsonUser = 'keyser';
var jsonPassword = 'soze';
这是问题代码
// Initialize the JSONStore
WL.JSONStore.init(collections)
.then(function() {
console.log("store initialized");
// query the store
var query = {_id: 0};
WL.JSONStore.get(collectionName)
.find(query)
.then(function(arrayResults) {
console.log("credentials retrieved " + arrayResults.length);
if (arrayResults.length > 0) {
// retrieve the credentials from the json object
console.log("password retrieved " + arrayResults[0].json.password);
jsonURL = arrayResults[0].json.url;
jsonUser = arrayResults[0].json.user;
jsonPassword = arrayResults[0].json.password;
} else {
// load the default credentials into jsonStore
var credentials = {url: jsonURL, user: jsonUser, password: jsonPassword};
WL.JSONStore.get(collectionName)
.add(credentials)
.then(function() {
console.log("credentials loaded " + credentials.url);
})
.fail(function(errorObject) {
console.log("credential load failed");
});
} // end of else
// Query the model list
queryModels();
}) // end of get(collectionName) then
.fail(function(errorObject) {
console.log("credentials not retrived");
}); // end of get(collectionName) fail
}) // end of init(collections) then
.fail(function(errorObject) {
console.log("store init failed" + errorObject);
}); // end of init(collections) fail
}); // end of ready
当我一步一步地通过它时,它按这个顺序流动
初始化(集合)
然后它立即跳到“准备就绪结束”。看起来很奇怪,但我是新手,所以也许没关系
返回get(collectionName)
到。然后,记录“检索到的凭据”,数组长度为0
到语句的else子句
它在else子句中的get(collectionName)上中断。到目前为止还不错
从这里跳到queryModels(),跳过.add(据我所知)
然后返回到。然后在第二个get下记录“已加载凭据”
此时执行“正常”结束,除非,
该项从未添加到集合中,并且
queryModels在我预期之前运行,我希望它在添加项之后运行
到目前为止,很明显我是一个菜鸟,所以我可能犯了菜鸟的错误。我知道
我在这里处理的是。然后和。失败的延迟,我正在嵌套它们,这似乎是错误的
一种公认的技术,但我没有得到我想要的执行顺序
我试过用两种格式注释第二个get(collections)的代码,但它都会有问题
// WL.JSONStore.get(collectionName)
.add(credentials)
及
非常感谢您的帮助。谢谢
下面是我的“答案”,这是我从下面的其他答案中学到的
Bluewing和cnandrue的回答都非常有用,我让它工作起来了。我发现的主要问题是
// Initialize the JSONStore - you have to .init to start the collection before you can read it.
WL.JSONStore.init(collections)
.then(function() {
console.log("store initialized");
// query the store
var query = {_id: 1};
WL.JSONStore.get(collectionName) // get 1
.find(query)
.then(function(arrayResults) {
console.log("credentials retrieved " + arrayResults.length);
if (arrayResults.length > 0) {
// retrieve the credentials from the json object
console.log("password retrieved " + arrayResults[0].json.password);
jsonURL = arrayResults[0].json.url;
jsonUser = arrayResults[0].json.user;
jsonPassword = arrayResults[0].json.password;
queryModels();
} else {
// load the default credentials into jsonStore
var credentials = {url: jsonURL, user: jsonUser, password: jsonPassword};
WL.JSONStore.get(collectionName) // get 2
.add(credentials)
.then(function(numberOfDocumentsAdded) {
console.log("Number of Docs Added" + numberOfDocumentsAdded);
queryModels();
}); // end of .add then
} // end of else
}); // end of get(collectionName) 1 then
}) // end of init(collections) then
.fail(function(errorObject) {
console.log("something failed" + errorObject);
}); // end of init(collections) fail
所有JSON存储调用(如add、init等)都是异步的。因此,只有在使用断点进行检查时,才会得到这种奇怪的流
要获得执行序列,请尝试移动queryModels()代码>加载凭据后
WL.JSONStore.get(collectionName)
.add(credentials)
.then(function() {
console.log("credentials loaded " + credentials.url);
queryModels();
})
我的建议与“蓝翼”相同,但我想分享一些伪代码:
函数handleCredentials(arrayResults,回调){
如果(arrayResults.length>0){
//..这里是同步代码。
setTimeout(函数(){
回调();
}, 0);
}否则{
WL.JSONStore.get(collectionName)
.add({url:jsonURL,用户:jsonUser,密码:jsonPassword})
.然后(函数(){
回调();
});
}
}
WL.JSONStore.init(集合)
.然后(函数(){
WL.JSONStore.get(collectionName)
.find({u id:1})
.then(函数(arrayResults){
handleCredentials(数组结果、函数(){
queryModels();
});
});
});
请注意,我为handleCredentials
创建了一个函数,该函数将执行同步操作(使用find
调用的结果设置一些变量)或异步操作(调用add
以添加凭据)。调用带有0的setTimeout以保留异步行为,对此进行了详细解释。handleCredentials
函数完成后,通过回调模式调用queryModels
函数
顺便说一句,我建议你读这篇博文:。特别是“错误处理”部分。您不需要在每个承诺中添加一个.fail
,您可以使用更少的失败函数,并且错误对象应该提供足够的详细信息来说明出错的原因。JSONStore错误对象是有文档记录的,请注意它们包含故障源(例如,src:'find'
) 我希望这是你想要的死刑。添加项目后,querymodels将运行。让我知道这是否有效
WL.JSONStore.get(collectionName)
.add(credentials)
.then(function() {
console.log("credentials loaded " + credentials.url);
queryModels();
})