我有一个函数,它在数组中包含一千个对象:
function Alltransaction(transactionArray) {
transactionArray.map(async (transaction) => {
dataAgainsthash = await web3.eth.getTransaction(transaction)
TransactionObject = {
transactionHash : transaction,
from : dataAgainsthash.from
};
transactionArray.push(TransactionObject)
console.log("transaction array", transactionArray)
});
}
然后我有另一个函数,它将这些数以千计的对象数组存储到数据库中
function saveTransactionToDb() {
console.log("after loop",transactionArray)
transactionss = new Transaction({
blockNumber : blockNumbers ,
transactions : transactionArray
})
// Now save the transaction to database
await transactionss.save();
// console.log("save to database")
}
然后我在我的路由器中调用这个
await Alltransaction(transactionArray);
await saveTransactionToDb();
我也试着
Alltransaction(transactionArray).then(saveTransactionToDb())
但它总是在对象数组填充
saveTransactionToDb()
方法之前运行Alltransaction()
。 最佳答案
在savetransactiontodb和alltransaction函数之前是否尝试async关键字?是吗?
async function Alltransaction(transactionArray){
// your code
}
async function saveTransactionToDb(){ // your code logic* await transactionss.save(); }