Home > other >  TypeScript function gives did not return a value, a promise, or throw an error
TypeScript function gives did not return a value, a promise, or throw an error

Time:04-13

trying to write a controller method for updating several documents in X collection, and pushes all array items to field of specicif document from Y collection. But when i try to make request i get error below

 Error: attendRef method did not return a value, a promise, or throw an error

BTW IT LOGS UNTIL CHECK4

unless i put return "THIS PART LOGS" to end. tried many solutions about it but really couldn't get the logic behind it, can someone help ? thx in advance.

 export const attendRef: Hapi.Lifecycle.Method = async (request, h, err) => {
      const payload: attendancePayload = <attendancePayload>request.payload;
      const id = request.auth.credentials.type;
      if (id == "ADMIN") {
        console.log("CHECK1");
        ApplicationModel.findById(payload.applicantId)
          .then(async (application) => {
            if (application) {
              console.log("CHECK2");
              if (application.attendedRefs.length < 3) {
                payload.refsId.forEach((element) => {
                  console.log("CHECK3");
                  RefereeModel.findById(element).then(async (ref) => {
                    if (ref) {
                      try {
                        await ref.updateOne(
                          { _id: ref.id },
                          { $push: { attendedApplicants: application.id } }
                        );
                      } catch (error) {
                        return Boom.badRequest(<string>error);
                      }
                    } else {
                      return Boom.badData("Hakem Bulunamadı");
                    }
                  });
                });
                console.log("CHECK4");
                await application.updateOne(
                  { _id: application.id },
                  { $push: { attendedRefs: payload.refsId } }
                );
    
                return h.response({
                  code: 200,
                  message: "Hakem Başarıyla Atandı",
                  data: { application: application, refs: payload.refsId },
                });
              } else {
                return Boom.badData("Bir başvuruya maksimum 3 hakem atanabilir");
              }
            } else {
              return Boom.badData("Katılımcı Bulunamadı");
            }
          })
          .catch((err) => {
          console.log(err);
            return new Error(<string>err);
          });
      } else {
          return Boom.unauthorized("ADMIN YETKISI GEREKLİ");
      }
     return "THIS PART LOGS"
    };

CodePudding user response:

You have to return your async call before returning it. Its because of the nature of the async calls.

When you call Application.findById, this method fetch few or lots of data depending on the data in the db after some time by calling then() or throws an error and you catch them by calling catch(). So it takes time.

Without return, your code does not return anything even then or catch blocks trigger.

export const attendRef: Hapi.Lifecycle.Method = async (request, h, err) => {
  const payload: attendancePayload = <attendancePayload>request.payload;
  const id = request.auth.credentials.type;
  if (id == "ADMIN") {
    console.log("CHECK1");
    return ApplicationModel.findById(payload.applicantId)
      .then(async (application) => {
        if (application) {
          console.log("CHECK2");
          if (application.attendedRefs.length < 3) {
            payload.refsId.forEach((element) => {
              console.log("CHECK3");
              RefereeModel.findById(element).then(async (ref) => {
                if (ref) {
                  try {
                    await ref.updateOne(
                      { _id: ref.id },
                      { $push: { attendedApplicants: application.id } }
                    );
                  } catch (error) {
                    return Boom.badRequest(<string>error);
                  }
                } else {
                  return Boom.badData("Hakem Bulunamadı");
                }
              });
            });
            console.log("CHECK4");
            await application.updateOne(
              { _id: application.id },
              { $push: { attendedRefs: payload.refsId } }
            );

            return h.response({
              code: 200,
              message: "Hakem Başarıyla Atandı",
              data: { application: application, refs: payload.refsId },
            });
          } else {
            return Boom.badData("Bir başvuruya maksimum 3 hakem atanabilir");
          }
        } else {
          return Boom.badData("Katılımcı Bulunamadı");
        }
      })
      .catch((err) => {
      console.log(err);
        return new Error(<string>err);
      });
  } else {
      return Boom.unauthorized("ADMIN YETKISI GEREKLİ");
  }
 //return "THIS PART LOGS"
};
  • Related