Sequelize - update record, and return resultUpdating model with Sequelize JSevent.preventDefault() vs. return falseWhy does ++[[]][+[]]+[+[]] return the string “10”?How to update a record using sequelize for node?How do I return the response from an asynchronous call?How do I update each dependency in package.json to the latest version?sequelize findOrCreate with include associationself.$expandAttributes is not a function with SequelizeSequelize updateSequelize model error-tring to make model in sequelize and getting this errorsequelize rollback not working with promise.all

If you attack a Tarrasque while swallowed, what AC do you need to beat to hit it?

How can I use 400 ASA film in a Leica IIIf, which does not have options higher than 100?

tikz: 5 squares on a row, roman numbered 1 -> 5

Salesforce bug enabled "Modify All"

Is there a realtime, uncut video of Saturn V ignition through tower clear?

400–430 degrees Celsius heated bath

How to safely discharge oneself

Is my company merging branches wrong?

Do most Taxis give Receipts in London?

Why is this python script running in background consuming 100 % CPU?

If the Charles SSL Proxy shows me sensitive data, is that data insecure/exposed?

Gambler's Fallacy Dice

Why use nominative in Coniugatio periphrastica passiva?

How can sister protect herself from impulse purchases with a credit card?

Is there any mention of ghosts who live outside the Hogwarts castle?

Mikrokosmos, BB 105, Vol. 1: No. 17 Contrary Motion (1) - Can't understand the structure

How can I prevent Bash expansion from passing files starting with "-" as argument?

Eigenvalues of the Laplace-Beltrami operator on a compact Riemannnian manifold

How did Jean Parisot de Valette, 49th Grand Master of the Order of Malta, die?

Good examples of "two is easy, three is hard" in computational sciences

Does the Aboleth have expertise in History and Perception?

why "American-born", not "America-born"?

How did Arya and the Hound get into King's Landing so easily?

Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?



Sequelize - update record, and return result


Updating model with Sequelize JSevent.preventDefault() vs. return falseWhy does ++[[]][+[]]+[+[]] return the string “10”?How to update a record using sequelize for node?How do I return the response from an asynchronous call?How do I update each dependency in package.json to the latest version?sequelize findOrCreate with include associationself.$expandAttributes is not a function with SequelizeSequelize updateSequelize model error-tring to make model in sequelize and getting this errorsequelize rollback not working with promise.all






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








32















I am using sequelize with MySQL. For example if I do:



models.People.update(OwnerId: peopleInfo.newuser,
where: id: peopleInfo.scenario.id)
.then(function (result)
response(result).code(200);

).catch(function (err)
request.server.log(['error'], err.stack);
).code(200);
);


I am not getting information back if the people model was succesfully updated or not. Variable result is just an array with one element, 0=1



How can I know for certain that the record was updated or not.










share|improve this question




























    32















    I am using sequelize with MySQL. For example if I do:



    models.People.update(OwnerId: peopleInfo.newuser,
    where: id: peopleInfo.scenario.id)
    .then(function (result)
    response(result).code(200);

    ).catch(function (err)
    request.server.log(['error'], err.stack);
    ).code(200);
    );


    I am not getting information back if the people model was succesfully updated or not. Variable result is just an array with one element, 0=1



    How can I know for certain that the record was updated or not.










    share|improve this question
























      32












      32








      32


      4






      I am using sequelize with MySQL. For example if I do:



      models.People.update(OwnerId: peopleInfo.newuser,
      where: id: peopleInfo.scenario.id)
      .then(function (result)
      response(result).code(200);

      ).catch(function (err)
      request.server.log(['error'], err.stack);
      ).code(200);
      );


      I am not getting information back if the people model was succesfully updated or not. Variable result is just an array with one element, 0=1



      How can I know for certain that the record was updated or not.










      share|improve this question














      I am using sequelize with MySQL. For example if I do:



      models.People.update(OwnerId: peopleInfo.newuser,
      where: id: peopleInfo.scenario.id)
      .then(function (result)
      response(result).code(200);

      ).catch(function (err)
      request.server.log(['error'], err.stack);
      ).code(200);
      );


      I am not getting information back if the people model was succesfully updated or not. Variable result is just an array with one element, 0=1



      How can I know for certain that the record was updated or not.







      javascript sequelize.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 22 '16 at 11:08









      Vedran Maricevic.Vedran Maricevic.

      2,49643662




      2,49643662






















          5 Answers
          5






          active

          oldest

          votes


















          59














          Here's what I think you're looking for.



          db.connections.update(
          user: data.username,
          chatroomID: data.chatroomID
          ,
          where: socketID: socket.id ,
          returning: true,
          plain: true
          )
          .then(function (result)
          console.log(result);
          // result = [x] or [x, y]
          // [x] if you're not using Postgres
          // [x, y] if you are using Postgres
          );


          From Sequelize docs:
          The promise returns an array with one or two elements. The first element x is always the number of affected rows, while the second element y is the actual affected rows (only supported in postgres with options.returning set to true.)



          Assuming you are using Postgres, you can access the updated object with result[1].dataValues.



          You must set returning: true option to tell Sequelize to return the object. And plain: true is just to return the object itself and not the other messy meta data that might not be useful.






          share|improve this answer




















          • 1





            I am using MySQL. I will test it with. Thanks for reply

            – Vedran Maricevic.
            Nov 11 '16 at 7:53











          • @Wexoni is it work using MySQL? give me feedback

            – MH Rohman Masyhar
            Dec 12 '16 at 11:14






          • 4





            I was not able to make it work with MySql. I guess this is only Postgresql feature :(

            – Vedran Maricevic.
            Dec 12 '16 at 11:16






          • 2





            Also, despite what the docs say, I get undefined as the first parameter and the number of affected rows as the second parameter (using sqlite). To fix this I did result = result.filter(Boolean); before processing anything.

            – Ryan Wheale
            Jun 28 '17 at 20:42











          • Is there a way to specify returned columns? I'm working with Postgress

            – Uri Abramson
            Oct 2 '17 at 17:36


















          7














          Update function of sequelize returns a number of affected rows (first parameter of result array).



          You should call find to get updated row



          models.People.update(OwnerId: peopleInfo.newuser,
          where: id: peopleInfo.scenario.id)
          .then(function (result)
          models.People.findById(peopleInfo.scenario.id)
          .then(function(user)
          response(user).code(200);
          ).catch(function (err)
          request.server.log(['error'], err.stack);
          ).code(200);
          );
          ).catch(function (err)
          request.server.log(['error'], err.stack);
          ).code(200);
          );





          share|improve this answer


















          • 1





            So it seems the only way is to call find immediately after the update?

            – Vedran Maricevic.
            Jul 25 '16 at 11:20











          • anyway you should use find function.

            – Tilekbekov Yrysbek
            Jul 25 '16 at 12:05






          • 2





            you can call find function first, then do update with this object without using where

            – Tilekbekov Yrysbek
            Jul 25 '16 at 12:07






          • 1





            if you're using PostgreSQL you can define returning option to true that returns affected rows

            – Tilekbekov Yrysbek
            Jul 25 '16 at 12:11


















          1














          same thing you can do with async-await, especially to avoid nested Promises
          You just need to create async function :)



          const asyncFunction = async function(req, res) 
          try
          //update
          const updatePeople = await models.People.update(OwnerId: peopleInfo.newuser,
          where: id: peopleInfo.scenario.id)
          if (!updatePeople) throw ('Error while Updating');
          // fetch updated data
          const returnUpdatedPerson = await models.People.findById(peopleInfo.scenario.id)
          if(!returnUpdatedPerson) throw ('Error while Fetching Data');
          res(user).code(200);
          catch (error)
          res.send(error)







          share|improve this answer
































            0














            You can just find the item and update its properties and then save it.
            The save() results in a UPDATE query to the db



             const job = await Job.findOne(where: id, ownerId: req.user.id);
            if (!job)
            throw Error(`Job not updated. id: $id`);


            job.name = input.name;
            job.payload = input.payload;
            await job.save();


            On Postgres:



            Executing (default): UPDATE "jobs" SET "payload"=$1,"updatedAt"=$2 WHERE "id" = $3





            share|improve this answer






























              0














              Simple snipet to update and get updated result



              models.People.findOne(
              where: id: peopleInfo.scenario.id
              )
              .then((people) =>
              if(people == null) console.log("invalid people");
              people.fieldOne = currentValue;
              people.fieldtwo = currentValue;
              return people.save()
              )





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38524938%2fsequelize-update-record-and-return-result%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                59














                Here's what I think you're looking for.



                db.connections.update(
                user: data.username,
                chatroomID: data.chatroomID
                ,
                where: socketID: socket.id ,
                returning: true,
                plain: true
                )
                .then(function (result)
                console.log(result);
                // result = [x] or [x, y]
                // [x] if you're not using Postgres
                // [x, y] if you are using Postgres
                );


                From Sequelize docs:
                The promise returns an array with one or two elements. The first element x is always the number of affected rows, while the second element y is the actual affected rows (only supported in postgres with options.returning set to true.)



                Assuming you are using Postgres, you can access the updated object with result[1].dataValues.



                You must set returning: true option to tell Sequelize to return the object. And plain: true is just to return the object itself and not the other messy meta data that might not be useful.






                share|improve this answer




















                • 1





                  I am using MySQL. I will test it with. Thanks for reply

                  – Vedran Maricevic.
                  Nov 11 '16 at 7:53











                • @Wexoni is it work using MySQL? give me feedback

                  – MH Rohman Masyhar
                  Dec 12 '16 at 11:14






                • 4





                  I was not able to make it work with MySql. I guess this is only Postgresql feature :(

                  – Vedran Maricevic.
                  Dec 12 '16 at 11:16






                • 2





                  Also, despite what the docs say, I get undefined as the first parameter and the number of affected rows as the second parameter (using sqlite). To fix this I did result = result.filter(Boolean); before processing anything.

                  – Ryan Wheale
                  Jun 28 '17 at 20:42











                • Is there a way to specify returned columns? I'm working with Postgress

                  – Uri Abramson
                  Oct 2 '17 at 17:36















                59














                Here's what I think you're looking for.



                db.connections.update(
                user: data.username,
                chatroomID: data.chatroomID
                ,
                where: socketID: socket.id ,
                returning: true,
                plain: true
                )
                .then(function (result)
                console.log(result);
                // result = [x] or [x, y]
                // [x] if you're not using Postgres
                // [x, y] if you are using Postgres
                );


                From Sequelize docs:
                The promise returns an array with one or two elements. The first element x is always the number of affected rows, while the second element y is the actual affected rows (only supported in postgres with options.returning set to true.)



                Assuming you are using Postgres, you can access the updated object with result[1].dataValues.



                You must set returning: true option to tell Sequelize to return the object. And plain: true is just to return the object itself and not the other messy meta data that might not be useful.






                share|improve this answer




















                • 1





                  I am using MySQL. I will test it with. Thanks for reply

                  – Vedran Maricevic.
                  Nov 11 '16 at 7:53











                • @Wexoni is it work using MySQL? give me feedback

                  – MH Rohman Masyhar
                  Dec 12 '16 at 11:14






                • 4





                  I was not able to make it work with MySql. I guess this is only Postgresql feature :(

                  – Vedran Maricevic.
                  Dec 12 '16 at 11:16






                • 2





                  Also, despite what the docs say, I get undefined as the first parameter and the number of affected rows as the second parameter (using sqlite). To fix this I did result = result.filter(Boolean); before processing anything.

                  – Ryan Wheale
                  Jun 28 '17 at 20:42











                • Is there a way to specify returned columns? I'm working with Postgress

                  – Uri Abramson
                  Oct 2 '17 at 17:36













                59












                59








                59







                Here's what I think you're looking for.



                db.connections.update(
                user: data.username,
                chatroomID: data.chatroomID
                ,
                where: socketID: socket.id ,
                returning: true,
                plain: true
                )
                .then(function (result)
                console.log(result);
                // result = [x] or [x, y]
                // [x] if you're not using Postgres
                // [x, y] if you are using Postgres
                );


                From Sequelize docs:
                The promise returns an array with one or two elements. The first element x is always the number of affected rows, while the second element y is the actual affected rows (only supported in postgres with options.returning set to true.)



                Assuming you are using Postgres, you can access the updated object with result[1].dataValues.



                You must set returning: true option to tell Sequelize to return the object. And plain: true is just to return the object itself and not the other messy meta data that might not be useful.






                share|improve this answer















                Here's what I think you're looking for.



                db.connections.update(
                user: data.username,
                chatroomID: data.chatroomID
                ,
                where: socketID: socket.id ,
                returning: true,
                plain: true
                )
                .then(function (result)
                console.log(result);
                // result = [x] or [x, y]
                // [x] if you're not using Postgres
                // [x, y] if you are using Postgres
                );


                From Sequelize docs:
                The promise returns an array with one or two elements. The first element x is always the number of affected rows, while the second element y is the actual affected rows (only supported in postgres with options.returning set to true.)



                Assuming you are using Postgres, you can access the updated object with result[1].dataValues.



                You must set returning: true option to tell Sequelize to return the object. And plain: true is just to return the object itself and not the other messy meta data that might not be useful.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 30 '18 at 13:48









                Yves M.

                19.5k1373104




                19.5k1373104










                answered Nov 11 '16 at 7:51









                nickangnickang

                711412




                711412







                • 1





                  I am using MySQL. I will test it with. Thanks for reply

                  – Vedran Maricevic.
                  Nov 11 '16 at 7:53











                • @Wexoni is it work using MySQL? give me feedback

                  – MH Rohman Masyhar
                  Dec 12 '16 at 11:14






                • 4





                  I was not able to make it work with MySql. I guess this is only Postgresql feature :(

                  – Vedran Maricevic.
                  Dec 12 '16 at 11:16






                • 2





                  Also, despite what the docs say, I get undefined as the first parameter and the number of affected rows as the second parameter (using sqlite). To fix this I did result = result.filter(Boolean); before processing anything.

                  – Ryan Wheale
                  Jun 28 '17 at 20:42











                • Is there a way to specify returned columns? I'm working with Postgress

                  – Uri Abramson
                  Oct 2 '17 at 17:36












                • 1





                  I am using MySQL. I will test it with. Thanks for reply

                  – Vedran Maricevic.
                  Nov 11 '16 at 7:53











                • @Wexoni is it work using MySQL? give me feedback

                  – MH Rohman Masyhar
                  Dec 12 '16 at 11:14






                • 4





                  I was not able to make it work with MySql. I guess this is only Postgresql feature :(

                  – Vedran Maricevic.
                  Dec 12 '16 at 11:16






                • 2





                  Also, despite what the docs say, I get undefined as the first parameter and the number of affected rows as the second parameter (using sqlite). To fix this I did result = result.filter(Boolean); before processing anything.

                  – Ryan Wheale
                  Jun 28 '17 at 20:42











                • Is there a way to specify returned columns? I'm working with Postgress

                  – Uri Abramson
                  Oct 2 '17 at 17:36







                1




                1





                I am using MySQL. I will test it with. Thanks for reply

                – Vedran Maricevic.
                Nov 11 '16 at 7:53





                I am using MySQL. I will test it with. Thanks for reply

                – Vedran Maricevic.
                Nov 11 '16 at 7:53













                @Wexoni is it work using MySQL? give me feedback

                – MH Rohman Masyhar
                Dec 12 '16 at 11:14





                @Wexoni is it work using MySQL? give me feedback

                – MH Rohman Masyhar
                Dec 12 '16 at 11:14




                4




                4





                I was not able to make it work with MySql. I guess this is only Postgresql feature :(

                – Vedran Maricevic.
                Dec 12 '16 at 11:16





                I was not able to make it work with MySql. I guess this is only Postgresql feature :(

                – Vedran Maricevic.
                Dec 12 '16 at 11:16




                2




                2





                Also, despite what the docs say, I get undefined as the first parameter and the number of affected rows as the second parameter (using sqlite). To fix this I did result = result.filter(Boolean); before processing anything.

                – Ryan Wheale
                Jun 28 '17 at 20:42





                Also, despite what the docs say, I get undefined as the first parameter and the number of affected rows as the second parameter (using sqlite). To fix this I did result = result.filter(Boolean); before processing anything.

                – Ryan Wheale
                Jun 28 '17 at 20:42













                Is there a way to specify returned columns? I'm working with Postgress

                – Uri Abramson
                Oct 2 '17 at 17:36





                Is there a way to specify returned columns? I'm working with Postgress

                – Uri Abramson
                Oct 2 '17 at 17:36













                7














                Update function of sequelize returns a number of affected rows (first parameter of result array).



                You should call find to get updated row



                models.People.update(OwnerId: peopleInfo.newuser,
                where: id: peopleInfo.scenario.id)
                .then(function (result)
                models.People.findById(peopleInfo.scenario.id)
                .then(function(user)
                response(user).code(200);
                ).catch(function (err)
                request.server.log(['error'], err.stack);
                ).code(200);
                );
                ).catch(function (err)
                request.server.log(['error'], err.stack);
                ).code(200);
                );





                share|improve this answer


















                • 1





                  So it seems the only way is to call find immediately after the update?

                  – Vedran Maricevic.
                  Jul 25 '16 at 11:20











                • anyway you should use find function.

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:05






                • 2





                  you can call find function first, then do update with this object without using where

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:07






                • 1





                  if you're using PostgreSQL you can define returning option to true that returns affected rows

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:11















                7














                Update function of sequelize returns a number of affected rows (first parameter of result array).



                You should call find to get updated row



                models.People.update(OwnerId: peopleInfo.newuser,
                where: id: peopleInfo.scenario.id)
                .then(function (result)
                models.People.findById(peopleInfo.scenario.id)
                .then(function(user)
                response(user).code(200);
                ).catch(function (err)
                request.server.log(['error'], err.stack);
                ).code(200);
                );
                ).catch(function (err)
                request.server.log(['error'], err.stack);
                ).code(200);
                );





                share|improve this answer


















                • 1





                  So it seems the only way is to call find immediately after the update?

                  – Vedran Maricevic.
                  Jul 25 '16 at 11:20











                • anyway you should use find function.

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:05






                • 2





                  you can call find function first, then do update with this object without using where

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:07






                • 1





                  if you're using PostgreSQL you can define returning option to true that returns affected rows

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:11













                7












                7








                7







                Update function of sequelize returns a number of affected rows (first parameter of result array).



                You should call find to get updated row



                models.People.update(OwnerId: peopleInfo.newuser,
                where: id: peopleInfo.scenario.id)
                .then(function (result)
                models.People.findById(peopleInfo.scenario.id)
                .then(function(user)
                response(user).code(200);
                ).catch(function (err)
                request.server.log(['error'], err.stack);
                ).code(200);
                );
                ).catch(function (err)
                request.server.log(['error'], err.stack);
                ).code(200);
                );





                share|improve this answer













                Update function of sequelize returns a number of affected rows (first parameter of result array).



                You should call find to get updated row



                models.People.update(OwnerId: peopleInfo.newuser,
                where: id: peopleInfo.scenario.id)
                .then(function (result)
                models.People.findById(peopleInfo.scenario.id)
                .then(function(user)
                response(user).code(200);
                ).catch(function (err)
                request.server.log(['error'], err.stack);
                ).code(200);
                );
                ).catch(function (err)
                request.server.log(['error'], err.stack);
                ).code(200);
                );






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 25 '16 at 11:19









                Tilekbekov YrysbekTilekbekov Yrysbek

                1,51238




                1,51238







                • 1





                  So it seems the only way is to call find immediately after the update?

                  – Vedran Maricevic.
                  Jul 25 '16 at 11:20











                • anyway you should use find function.

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:05






                • 2





                  you can call find function first, then do update with this object without using where

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:07






                • 1





                  if you're using PostgreSQL you can define returning option to true that returns affected rows

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:11












                • 1





                  So it seems the only way is to call find immediately after the update?

                  – Vedran Maricevic.
                  Jul 25 '16 at 11:20











                • anyway you should use find function.

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:05






                • 2





                  you can call find function first, then do update with this object without using where

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:07






                • 1





                  if you're using PostgreSQL you can define returning option to true that returns affected rows

                  – Tilekbekov Yrysbek
                  Jul 25 '16 at 12:11







                1




                1





                So it seems the only way is to call find immediately after the update?

                – Vedran Maricevic.
                Jul 25 '16 at 11:20





                So it seems the only way is to call find immediately after the update?

                – Vedran Maricevic.
                Jul 25 '16 at 11:20













                anyway you should use find function.

                – Tilekbekov Yrysbek
                Jul 25 '16 at 12:05





                anyway you should use find function.

                – Tilekbekov Yrysbek
                Jul 25 '16 at 12:05




                2




                2





                you can call find function first, then do update with this object without using where

                – Tilekbekov Yrysbek
                Jul 25 '16 at 12:07





                you can call find function first, then do update with this object without using where

                – Tilekbekov Yrysbek
                Jul 25 '16 at 12:07




                1




                1





                if you're using PostgreSQL you can define returning option to true that returns affected rows

                – Tilekbekov Yrysbek
                Jul 25 '16 at 12:11





                if you're using PostgreSQL you can define returning option to true that returns affected rows

                – Tilekbekov Yrysbek
                Jul 25 '16 at 12:11











                1














                same thing you can do with async-await, especially to avoid nested Promises
                You just need to create async function :)



                const asyncFunction = async function(req, res) 
                try
                //update
                const updatePeople = await models.People.update(OwnerId: peopleInfo.newuser,
                where: id: peopleInfo.scenario.id)
                if (!updatePeople) throw ('Error while Updating');
                // fetch updated data
                const returnUpdatedPerson = await models.People.findById(peopleInfo.scenario.id)
                if(!returnUpdatedPerson) throw ('Error while Fetching Data');
                res(user).code(200);
                catch (error)
                res.send(error)







                share|improve this answer





























                  1














                  same thing you can do with async-await, especially to avoid nested Promises
                  You just need to create async function :)



                  const asyncFunction = async function(req, res) 
                  try
                  //update
                  const updatePeople = await models.People.update(OwnerId: peopleInfo.newuser,
                  where: id: peopleInfo.scenario.id)
                  if (!updatePeople) throw ('Error while Updating');
                  // fetch updated data
                  const returnUpdatedPerson = await models.People.findById(peopleInfo.scenario.id)
                  if(!returnUpdatedPerson) throw ('Error while Fetching Data');
                  res(user).code(200);
                  catch (error)
                  res.send(error)







                  share|improve this answer



























                    1












                    1








                    1







                    same thing you can do with async-await, especially to avoid nested Promises
                    You just need to create async function :)



                    const asyncFunction = async function(req, res) 
                    try
                    //update
                    const updatePeople = await models.People.update(OwnerId: peopleInfo.newuser,
                    where: id: peopleInfo.scenario.id)
                    if (!updatePeople) throw ('Error while Updating');
                    // fetch updated data
                    const returnUpdatedPerson = await models.People.findById(peopleInfo.scenario.id)
                    if(!returnUpdatedPerson) throw ('Error while Fetching Data');
                    res(user).code(200);
                    catch (error)
                    res.send(error)







                    share|improve this answer















                    same thing you can do with async-await, especially to avoid nested Promises
                    You just need to create async function :)



                    const asyncFunction = async function(req, res) 
                    try
                    //update
                    const updatePeople = await models.People.update(OwnerId: peopleInfo.newuser,
                    where: id: peopleInfo.scenario.id)
                    if (!updatePeople) throw ('Error while Updating');
                    // fetch updated data
                    const returnUpdatedPerson = await models.People.findById(peopleInfo.scenario.id)
                    if(!returnUpdatedPerson) throw ('Error while Fetching Data');
                    res(user).code(200);
                    catch (error)
                    res.send(error)








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 5 at 13:47

























                    answered Mar 23 at 19:33









                    Bharat SuryawanshiBharat Suryawanshi

                    213




                    213





















                        0














                        You can just find the item and update its properties and then save it.
                        The save() results in a UPDATE query to the db



                         const job = await Job.findOne(where: id, ownerId: req.user.id);
                        if (!job)
                        throw Error(`Job not updated. id: $id`);


                        job.name = input.name;
                        job.payload = input.payload;
                        await job.save();


                        On Postgres:



                        Executing (default): UPDATE "jobs" SET "payload"=$1,"updatedAt"=$2 WHERE "id" = $3





                        share|improve this answer



























                          0














                          You can just find the item and update its properties and then save it.
                          The save() results in a UPDATE query to the db



                           const job = await Job.findOne(where: id, ownerId: req.user.id);
                          if (!job)
                          throw Error(`Job not updated. id: $id`);


                          job.name = input.name;
                          job.payload = input.payload;
                          await job.save();


                          On Postgres:



                          Executing (default): UPDATE "jobs" SET "payload"=$1,"updatedAt"=$2 WHERE "id" = $3





                          share|improve this answer

























                            0












                            0








                            0







                            You can just find the item and update its properties and then save it.
                            The save() results in a UPDATE query to the db



                             const job = await Job.findOne(where: id, ownerId: req.user.id);
                            if (!job)
                            throw Error(`Job not updated. id: $id`);


                            job.name = input.name;
                            job.payload = input.payload;
                            await job.save();


                            On Postgres:



                            Executing (default): UPDATE "jobs" SET "payload"=$1,"updatedAt"=$2 WHERE "id" = $3





                            share|improve this answer













                            You can just find the item and update its properties and then save it.
                            The save() results in a UPDATE query to the db



                             const job = await Job.findOne(where: id, ownerId: req.user.id);
                            if (!job)
                            throw Error(`Job not updated. id: $id`);


                            job.name = input.name;
                            job.payload = input.payload;
                            await job.save();


                            On Postgres:



                            Executing (default): UPDATE "jobs" SET "payload"=$1,"updatedAt"=$2 WHERE "id" = $3






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 3 at 2:25









                            David DehghanDavid Dehghan

                            6,58614862




                            6,58614862





















                                0














                                Simple snipet to update and get updated result



                                models.People.findOne(
                                where: id: peopleInfo.scenario.id
                                )
                                .then((people) =>
                                if(people == null) console.log("invalid people");
                                people.fieldOne = currentValue;
                                people.fieldtwo = currentValue;
                                return people.save()
                                )





                                share|improve this answer



























                                  0














                                  Simple snipet to update and get updated result



                                  models.People.findOne(
                                  where: id: peopleInfo.scenario.id
                                  )
                                  .then((people) =>
                                  if(people == null) console.log("invalid people");
                                  people.fieldOne = currentValue;
                                  people.fieldtwo = currentValue;
                                  return people.save()
                                  )





                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    Simple snipet to update and get updated result



                                    models.People.findOne(
                                    where: id: peopleInfo.scenario.id
                                    )
                                    .then((people) =>
                                    if(people == null) console.log("invalid people");
                                    people.fieldOne = currentValue;
                                    people.fieldtwo = currentValue;
                                    return people.save()
                                    )





                                    share|improve this answer













                                    Simple snipet to update and get updated result



                                    models.People.findOne(
                                    where: id: peopleInfo.scenario.id
                                    )
                                    .then((people) =>
                                    if(people == null) console.log("invalid people");
                                    people.fieldOne = currentValue;
                                    people.fieldtwo = currentValue;
                                    return people.save()
                                    )






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered May 6 at 8:05









                                    Riajul IslamRiajul Islam

                                    473511




                                    473511



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38524938%2fsequelize-update-record-and-return-result%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                        SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                        은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현