Это, пожалуй, самая странная вещь, которая когда-либо случалась со мной с тех пор, как я начал кодировать:
Сценарий 1: Await используется в начале функции
const resObj = operationsSav.map(async operationSav => {
Asyncproduit = await operationSav.getProduit();
logger.error(
"retrieved produit by using Async is",
Asyncproduit.nom_produit
);
const mapStatusToDisplayedValue = {
produit_recu: "Produit reçu",
en_livraison: "En livraison",
en_reparation: "En cours de réparation",
repare: "Réparé",
en_expedition: "Envoyé au client",
expedie: "Reçu par le client",
annule: "Annulé"
};
return Object.assign({
id: operationSav.id,
nom_operation: operationSav.nom_operation,
num_serie: operationSav.num_serie,
address_mac: operationSav.address_mac,
sous_garantie: operationSav.sous_garantie,
description: operationSav.description,
admin: admin.nom,
produitNom: Asyncproduit.nom_produit,
status: mapStatusToDisplayedValue[operationSav.status],
nomClient: operationSav.nomClient
});
});
const operationSavToDisplay = await Promise.all(resObj);
В этом сценарии: извлеченное Asyncproduit
всегда совпадает с тем, что вы видите в результате записи в журнале:
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
Executing (default): SELECT "id", "nom", "email", "mot_de_passe", "last_login", "role", "createdAt", "updatedAt" FROM "admins" AS "admin" WHERE "admin"."id" = 75;
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
Executing (default): SELECT "id", "nom", "email", "mot_de_passe", "last_login", "role", "createdAt", "updatedAt" FROM "admins" AS "admin" WHERE "admin"."id" = 75;
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
2020-03-04T09:36:40+0100 <error> OperationSavServices.js:121 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
Сценарий 2: Ожидание используется в середине функции
const resObj = operationsSav.map(async operationSav => {
const mapStatusToDisplayedValue = {
produit_recu: "Produit reçu",
en_livraison: "En livraison",
en_reparation: "En cours de réparation",
repare: "Réparé",
en_expedition: "Envoyé au client",
expedie: "Reçu par le client",
annule: "Annulé"
};
/** TESTING GET PRODUIT WITH ASYNC AWAIT */
Asyncproduit = await operationSav.getProduit();
logger.error(
"retrieved produit by using Async is",
Asyncproduit.nom_produit
);
/** TESTING GET PRODUIT WITH ASYNC AWAIT */
return Object.assign({
id: operationSav.id,
nom_operation: operationSav.nom_operation,
num_serie: operationSav.num_serie,
address_mac: operationSav.address_mac,
sous_garantie: operationSav.sous_garantie,
description: operationSav.description,
admin: admin.nom,
produitNom: Asyncproduit.nom_produit,
status: mapStatusToDisplayedValue[operationSav.status],
nomClient: operationSav.nomClient
});
});
const operationSavToDisplay = await Promise.all(resObj);
В этом сценарии: извлеченный Asyncproduit
отличается, как вы можете видеть в результате журнала:
2020-03-04T09:43:02+0100 <error> OperationSavServices.js:93 retrieved produit by using Async is + PURE LIMITED CHROME GOLD MEMORY 256G
2020-03-04T09:43:02+0100 <error> OperationSavServices.js:93 retrieved produit by using Async is + PURE PLUS BLACK MAT MEMORY 128 G
2020-03-04T09:43:02+0100 <error> OperationSavServices.js:93 retrieved produit by using Async is + PURE PLUS BLACK MAT MEMORY 128 G
2020-03-04T09:43:02+0100 <error> OperationSavServices.js:93 retrieved produit by using Async is + PURE LIMITED CHROME GOLD MEMORY 256G
2020-03-04T09:43:02+0100 <error> OperationSavServices.js:93 retrieved produit by using Async is + GAME ONE WHITE
2020-03-04T09:43:02+0100 <error> OperationSavServices.js:93 retrieved produit by using Async is + PLAYER BLACK MAT FILAIRE
2020-03-04T09:43:02+0100 <error> OperationSavServices.js:93 retrieved produit by using Async is + PLAYER GOLD MAT FILAIRE
2020-03-04T09:43:02+0100 <error> OperationSavServices.js:93 retrieved produit by using Async is + GAME ONE WHITE
Результаты в Сценарий 2 являются правильными результатами . Связь между operationsSav
и produit
определяется следующим образом:
/** produit Has Many operation_sav*/
model.operation_sav.belongsTo(model.produit, {
foreignKeyConstraint: true,
onDelete: "cascade"
});
model.produit.hasMany(model.operation_sav, {
foreignKeyConstraint: true,
onDelete: "cascade"
});
Это означает, что каждая строка в таблице operation_sav
связана со строкой в таблице produit
. Но , это не значит, что produit
всегда одинаков. Каждая строка operation_sav
может быть связана с различным produit
, что имеет место в моей базе данных, как доказывает Сценарий 2 .
Я был сбит с толку, почему база данных возвращала тот же produit
в сценарии один, и я решил это с чистой удачей.
Теперь , я хотел бы понять, почему код ведет себя по-разному в двух сценариях ios!
РЕДАКТИРОВАТЬ 1 Это весь код. Обратите внимание на первые строки:
const resObj = operationsSav.map(async operationSav => {
// This line wasn't written in the code above, maybe it was causing the problem?
admin = await operationSav.getAdmin();
/** TESTING GET PRODUIT WITH ASYNC AWAIT */
Asyncproduit = await operationSav.getProduit();
logger.error(
"retrieved produit by using Async is",
Asyncproduit.nom_produit
);
/** TESTING GET PRODUIT WITH ASYNC AWAIT */
const mapStatusToDisplayedValue = {
produit_recu: "Produit reçu",
en_livraison: "En livraison",
en_reparation: "En cours de réparation",
repare: "Réparé",
en_expedition: "Envoyé au client",
expedie: "Reçu par le client",
annule: "Annulé"
};
return Object.assign({
id: operationSav.id,
nom_operation: operationSav.nom_operation,
num_serie: operationSav.num_serie,
address_mac: operationSav.address_mac,
sous_garantie: operationSav.sous_garantie,
description: operationSav.description,
admin: admin.nom,
produitNom: Asyncproduit.nom_produit,
status: mapStatusToDisplayedValue[operationSav.status],
nomClient: operationSav.nomClient
});
});
const operationSavToDisplay = await Promise.all(resObj);