REACT NATIVE 疑问:尝试读取数据库并创建内容数组。在代码中,更新数组“rcpts”后,尝试访问 db.transaction 块外部时,它仍为空。如何真正更新该块内的数组?我问 bcz,似乎即使在 db.transaction 块的 while 循环内更新后,数组仍为空或保持其原始形式。
let rcpts = []
const db = SQLite.openDatabase('digit.db');
//date, agentNo, custName, custNo, paidAmount, custBalance, payMode, revGroup FROM tblRcpt ORDER BY Id DESC LIMIT 100
function pushreceipts(){
let rcptlist =[];
//Alert.alert("you clicked");
db.transaction(tx=>{
tx.executeSql("SELECT id,date, agentNo, custName, custNo, paidAmount, custBalance, payMode, revGroup FROM tblRcpt ORDER BY Id DESC LIMIT 100 ",
null,(tx,result)=>{
let len = result.rows.length;
if (len>0){
for(let i =0;i<len;i++){
let item = result.rows.item(i);
rcptlist.push({
rcptNo : item.id,
rcptdate : item.date,
agentNo : item.agentNo,
custName : item.custName,
custNo : item.custNo,
paidAmount : item.paidAmount,
custBalance : item.custBalance,
payMode : item.payMode,
revGroup : item.revGroup
});
i++;
}
}
rcpts = rcptlist;
Alert.alert("Data Push finished.")
//Alert.alert("Survey Added");
}),
(error) =>{
Alert.alert(error);
}
}
)
// **** Read the rcps Array and send using fetch here. ****
let qty = rcpts.length;
let i = 0;
console.log(rcpts.length)
while(i<qty+1){
console.log('length of rcpts array'+rcpts.length);
i++;
}
}
回答你的问题;
你正在提供一个回调,但尝试访问数组,而没有确保回调已被调用。你可以这样做:
但是,对于您要做的事情,更合适的解决方案是使用 Expo SQLite;