如何解决Dexie 如何将结果赋值给变量?
我一直在尝试将 var 分配给结果而没有运气
self.addProductToCart = function (data,event) {
var theproduct = db.pos_products.get({id: data.idProduct}).then(function (pos_product) {
// console.log(pos_product.value); // returning results,which indicates it is working
return pos_product.value
}).catch(function(error) {
console.log ("Ooops: " + error);
});
console.log (theproduct); // retunrning no results only DexiePromise { listeners: Arrar(0),_lib.....
}
我也试过
async function myfunction(idProduct) {
let result = await db.pos_products.get({id: idProduct}).then(function (pos_product) {
return pos_product.value
}).catch(function(error) {
console.log ("Ooops: " + error);
});
}
self.addProductToCart = function (data,event) {
var theproduct = myfunction(data.idProduct);
console.log ('theproduct');
console.log (theproduct); // retunrning Promise {<pending>}
}
希望有人能帮忙。我需要变量 theproduct 稍后在 current 中使用
解决方法
我不知道为什么你的代码不起作用,但我经常看到初学者程序员不知道如何使用 async/await 和 promises,例如你等待 dexie 调用然后在它之后有一个 .then,我我很确定不玩得这么好? 但无论如何,下面的代码应该可以工作,只需查看 dexie 文档,这些简单的查询在那里都有很好的描述。
import re
----
pattern = r'[^\.a-z0-9]'
result = re.fullmatch(pattern,string)
if result:
return True
else
return False
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。