gpt4 book ai didi

angular - 如何使用 Angular 2返回 promise 的值(value)

转载 作者:太空狗 更新时间:2023-10-29 17:18:05 25 4
gpt4 key购买 nike

这是我的 promise 函数,我需要返回 rs.rows.item(0) 的值;

     public getCustomer()  : any
{
let db = window.sqlitePlugin.openDatabase({name: 'data.db', location: 'default'});
return new Promise((resolve, reject) =>
{
db.transaction(function(tx)
{
tx.executeSql('SELECT * FROM customer ORDER BY customerId DESC LIMIT 1', [], function(tx, rs)
{
return resolve(rs.rows.item(0));
},
function(tx, error)
{
console.log('SELECT error: ' + error.message);
reject(error);
});
});
});
}

返回值我得到了一个像这个图像的对象image result

我需要像这个例子一样

var customer = getCustomer();
customer.name;
customer.email;

最佳答案

Promises 为我们提供了抽象,帮助我们处理应用程序的异步特性。由于我们不知道这些操作需要多少时间(因此,数据何时可用),您需要使用 then() 方法在数据可用时执行一些代码准备好使用:

this.getCustomer()
.then((data) => {
// Here you can use the data because it's ready
// this.myVariable = data;
})
.catch((ex) => {
console.log(ex);
});

关于angular - 如何使用 Angular 2返回 promise 的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39124876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com