我需要运行一个if else
语句,然后在其中运行一个map,否则运行查询,但是它只是作为异步运行。
下面的代码已损坏。我不知道如何解决它。
function newRank() {
return new Promise((resolve, reject) => {
// If the select from target table is empty, then run this if below
if (rank_atual.length == 0) {
console.log('RANK ATUAL IF')
// Map new rank to add into target table
rank_gender.map(async (rnk) => {
// Query that will be feed by rank array and put that on the target table
let sql_insert = `INSERT INTO rnk_rank_diario (nr_rank_sem_genero, qt_tocadas, fk_id_fonogramas, fk_id_artista, fk_id_rnk_local_record, fk_id_rnk_tipo_emissora, fk_id_rnk_relevancia_local, fk_id_rnk_tipo_emissora_nr_relevancia, dh_sem_genero_criado, dh_sem_genero_alterado, nr_rank_com_genero, dh_com_genero_criado, dh_com_genero_alterado, fl_ativo, fk_id_fonogramas_genero, fl_record_sem_genero, fl_record_com_genero) VALUES (${rnk.rank}, ${rnk.total}, ${rnk.fonograma}, ${rnk.artista}, 1, ${rnk.categoria}, 5, 5, current_timestamp(), NULL, ${rnk.rank_genero}, current_timestamp(), NULL, 1, ${rnk.genero}, 1, 1)`;
// console.log(sql_insert)
// Run the query above, and check if everything goes fine.
conInsert.query(sql_insert, function (err, result) {
if (err) return reject(err);
})
})
}
else {
console.log('RANK ATUAL ELSE')
// Map new rank to add into target table
rank_gender.map((rnk) => {
// Find the index on the actual rank using the new rank fonogram
let ra = rank_atual.findIndex(obj => obj.fk_id_fonogramas == rnk.fonograma)
// if the position have been find, run the condition below
if (ra > 0) {
console.log(rnk.fonograma)
console.log(rank_atual[ra].fk_id_fonogramas)
console.log(rank_atual[ra].nr_rank_sem_genero)
// if the actual rank is less then NEW rank, then run the condition below
if (rnk.rank < rank_atual[ra].nr_rank_sem_genero && rnk.rank_genero < rank_atual[ra].nr_rank_com_genero ) {
// console.log(rank_atual[ra + 1].nr_rank_sem_genero)
// Query to update the old record changing the date and the flag to set inactive
let sql_update = `UPDATE rnk_rank_diario SET dh_sem_genero_alterado = current_timestamp(), dh_com_genero_alterado = current_timestamp(), fl_ativo = 0 WHERE fk_id_fonogramas = ${rank_atual[ra].nr_rank_sem_genero} AND fk_id_rnk_local_record = 1 AND fk_id_rnk_tipo_emissora = 5 AND fl_ativo = 1;`;
// Query to insert the new record with the new values.
let sql_insert = `INSERT INTO rnk_rank_diario (nr_rank_sem_genero, qt_tocadas, fk_id_fonogramas, fk_id_artista, fk_id_rnk_local_record, fk_id_rnk_tipo_emissora, fk_id_rnk_relevancia_local, fk_id_rnk_tipo_emissora_nr_relevancia, dh_sem_genero_criado, dh_sem_genero_alterado, nr_rank_com_genero, dh_com_genero_criado, dh_com_genero_alterado, fl_ativo, fk_id_fonogramas_genero, fl_record_sem_genero, fl_record_com_genero) VALUES (${rnk.rank}, ${rnk.total}, ${rnk.fonograma}, ${rnk.artista}, 1, ${rnk.categoria}, 5, 5, current_timestamp(), NULL, ${rnk.rank_genero}, '${moment(rank_atual[ra].dh_sem_genero_criado).utc().format('YYYY-MM-DD HH:MM:SS')}', NULL, 1, ${rnk.genero}, 1, 1);`;
// Run the update query, and check if everything goes fine.
conInsert.query(sql_insert, function (err, result) {
if (err) return reject(err)
});
// Run the insert query, and check if everything goes fine.
conInsert.query(sql_update, function (err, result) {
if (err) return reject(err)
});
}
} else {
console.log('OTHER ELSE')
// Query to create new records on the table if that song doesn't exist yet
let sql_insert = `INSERT INTO rnk_rank_diario (nr_rank_sem_genero, qt_tocadas, fk_id_fonogramas, fk_id_artista, fk_id_rnk_local_record, fk_id_rnk_tipo_emissora, fk_id_rnk_relevancia_local, fk_id_rnk_tipo_emissora_nr_relevancia, dh_sem_genero_criado, dh_sem_genero_alterado, nr_rank_com_genero, dh_com_genero_criado, dh_com_genero_alterado, fl_ativo, fk_id_fonogramas_genero, fl_record_sem_genero, fl_record_com_genero) VALUES (${rnk.rank}, ${rnk.total}, ${rnk.fonograma}, ${rnk.artista}, 1, ${rnk.categoria}, 5, 5, current_timestamp(), NULL, ${rnk.rank_genero}, current_timestamp(), NULL, 1, ${rnk.genero}, 1, 1);`;
// console.log(sql_insert)
// Run the query above, and check if everything goes fine.
conInsert.query(sql_insert, function (err, result) {
if (err) return reject(err);
})
}
})
}
呼叫者:
// Rodar as funções uma a uma.
async function mestre() {
// Function to get the new rank from rnkYYYYMM table
let result = await runSelectRank();
// Function to get the actual rank from rnk_rank_diario
result = await runSelectRank_Atual();
// Function to get the rank and insert them on the rank key inside of the objects
result = await getResult();
// Function to get the rank based on music gender
result = await getGenderResult();
// Function to verify and create the new ranks
result = await newRank();
// Function to close those connectios that were opened to insert into tables
result = await conInsert.end();
// Function to close those connectios that were opened to select from tables
result = await conSelect.end();
}
这是代码正在执行的示例,而不是等待完成并运行
30354
30354
776
18758
18758
946
43614
43614
498
28963
28963
839
9814
9814
834
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
OTHER ELSE
5. newRank rodou ok
当屏幕上显示5. newRank rodou ok
时,然后通过全部if else
语句传递后,查询开始运行,我是否丢失了某些内容?