我正在使用填海剂并努力工作,因为该方法永远处于待处理状态。
以下是我正在尝试做的简化版本。基本上,API通过调用应处理提供的json的BatchProcessor来利用以下方法。
我基本上希望BatchProcessor从FinalTheme方法中获取themeprice和themegate,但承诺永远都未定。
export default {
async FinalTheme(id) {
return db.Themes.findOne({
where: {
ID: id
},
attributes: ["ThemeCost","ThemeGate"],
limit: 1
})
.then(data => {
if (data == null) {
return -1;
}
return {
cost: data["ThemeCost"],
gate: data["ThemeGate"]
};
})
.catch(err => {
return false;
});
},
async BatchProcessor(record, index_number) {
const SQL ="SELECT * FROM themes";
return db.sequelize
.query(SQL, {
type: db.sequelize.QueryTypes.SELECT
})
.then(themes => {
// do we have data here?
const totalThemes = themes.length;
let lastAmount = record["Amount"];
for (
let counter = 0;
counter < totalThemes - 1;
counter++
) {
const CustomerFinalTheme = this.FinalTheme(record["CustomerID"]); // FOREVER PENDING
}
})
.catch(err => {
console.log(JSON.stringify(err));
});
},
};
我做错了什么?
答案 0 :(得分:1)
this.FinalTheme(...返回承诺,而不是您必须执行的值:
this.FinalTheme(record["CustomerId"]) // where is the record assigned?
.then(data => {
const CustomerFinalTheme = data;
})
在声明函数时也不需要使用异步,即可以进行以下操作:
FinalTheme(id) {
return db.Themes.findOne({
[...]
}
答案 1 :(得分:0)
您正在BatchProcessor的then块内部运行循环。您可以在内部等待循环。
async BatchProcessor(record, index_number) {
const SQL ="SELECT * FROM themes";
const themes = await db.sequelize.query(SQL, { type: db.sequelize.QueryTypes.SELECT });
const totalThemes = themes.length;
let lastAmount = record["Amount"];
for (let counter = 0; counter < totalThemes - 1; counter++) {
const CustomerFinalTheme = await this.FinalTheme(record["CustomerID"]);
}
return 'ALL DONE';
}