Navigation

    Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    I want to update database inside a for loop.But before my database update for loop ends. (#node js, #mongoose)

    Back-End Development
    nodejs mongodb
    4
    6
    904
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • kasun viduranga
      kasun viduranga last edited by

      var patientObj = JSON.parse(JSON.stringify(patients));

      for (var i = 0; i < patientObj.length; i++) {
      var patientIds = "00" + (i + 1);
      var patientNewIdObj = { barCodeId: patientIds}
      var currentPatientId = patientObj[i]._id;

      modelPatient.findOneAndUpdate({ _id: currentPatientId }, patientNewIdObj, 
      { new: true, safe: true }).exec(function (err, patientRet) {
          if (err) {
              callBack(callBackResponse.callbackWithDefaultError(err));
          }
          else if (patientRet == null || patientRet == undefined) {
              callBack(callBackResponse.callbackWithfalseMessage('Invalid 
              patient ID'));
          } else {
              callBack(callBackResponse.callbackWithData(patientRet));
          }
      });
      

      }

      1 Reply Last reply Reply Quote 0
      • Nubelle
        Nubelle Web Development last edited by

        @root FYI

        1 Reply Last reply Reply Quote 0
        • Danushka96
          Danushka96 last edited by

          Maybe that's because of the loop runs without waiting for your delete process finished. Try an await function.

          kasun viduranga 1 Reply Last reply Reply Quote 0
          • kasun viduranga
            kasun viduranga @Danushka96 last edited by

            how to use await function for this code.

            var patientObj = JSON.parse(JSON.stringify(patients));
            
            for (var i = 0; i < patientObj.length; i++) {
            var patientIds = "00" + (i + 1);
            var patientNewIdObj = { barCodeId: patientIds}
            var currentPatientId = patientObj[i]._id;
            
            modelPatient.findOneAndUpdate({ _id: currentPatientId }, patientNewIdObj, 
            { new: true, safe: true }).exec(function (err, patientRet) {
                if (err) {
                    callBack(callBackResponse.callbackWithDefaultError(err));
                }
                else if (patientRet == null || patientRet == undefined) {
                    callBack(callBackResponse.callbackWithfalseMessage('Invalid 
                    patient ID'));
                } else {
                    callBack(callBackResponse.callbackWithData(patientRet));
                }
            });
            }
            
            1 Reply Last reply Reply Quote 0
            • Danushka96
              Danushka96 last edited by

              check this link
              https://lavrton.com/javascript-loops-how-to-handle-async-await-6252dd3c795/

              1 Reply Last reply Reply Quote 0
              • ijseGovindu
                ijseGovindu last edited by

                https://javascript.info/async-await

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post

                1
                Online

                1.9k
                Users

                952
                Topics

                4.8k
                Posts

                • Privacy
                • Terms & Conditions
                • Donate
                • Contact Us

                © Copyrights and All right reserved Lanka Developers Community

                Sponsored by Axis Technologies (PVT) Ltd

                Made with in Sri Lanka

                | |