how to repeat the id for the length of an array in nodeJS and knexJSHow do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

Is "weekend warrior" derogatory?

Does publication of the phone call ruin the basis for impeachment?

Should I be an author on another PhD student's paper if I went to their meetings and gave advice?

Is it appropriate to "shop" through high-impact journals before sending the paper to more specialized journals?

How do French and other Romance language speakers cope with the movable do system?

Generating numbers with cubes

How are proofs normally constructed in a write up, in one line or split up into multiple lines?

How to protect bash function from being overridden?

Isn't the detector always measuring, and thus always collapsing the state?

As a team leader is it appropriate to bring in fundraiser candy?

Giving a good fancy look to a simple table

How to identify whether a publisher is genuine or not?

Everyone Gets a Window Seat

What is the difference between increasing volume and increasing gain?

Ĉi tie or ĉi-tie? Why do people sometimes hyphenate ĉi tie?

Why do popular TCP-using services have UDP as well as TCP entries in /etc/services?

Realistically, how much do you need to start investing?

How to say "respectively" in German when listing (enumerating) things

Did the Soviet army intentionally send troops (e.g. penal battalions) running over minefields?

Is spot metering just an EV compensation?

The difference of Prime in Solve doesn't work

Can I bring this power bank on board the aircraft?

How is this situation not a checkmate?

Can a passenger predict that an airline or a tour operator is about to go bankrupt?



how to repeat the id for the length of an array in nodeJS and knexJS


How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









2















I want to add an array of items with the same orderId, so I need to have the orderId repeated for the length of the array, how do I do that?



My code:



const handleOrdersPost = (req, res, db) => 
const customerId, orderStatus, productId, quantity, price = req.body;
if (!customerId

module.exports =
handleOrdersPost: handleOrdersPost
;


When I try to add multiple items using postman I get an error saying basically what I already know, that I need to repeat the id for the length of the array, because it works fine with just one item but I need it to work with multiple items.










share|improve this question
































    2















    I want to add an array of items with the same orderId, so I need to have the orderId repeated for the length of the array, how do I do that?



    My code:



    const handleOrdersPost = (req, res, db) => 
    const customerId, orderStatus, productId, quantity, price = req.body;
    if (!customerId

    module.exports =
    handleOrdersPost: handleOrdersPost
    ;


    When I try to add multiple items using postman I get an error saying basically what I already know, that I need to repeat the id for the length of the array, because it works fine with just one item but I need it to work with multiple items.










    share|improve this question




























      2












      2








      2


      1






      I want to add an array of items with the same orderId, so I need to have the orderId repeated for the length of the array, how do I do that?



      My code:



      const handleOrdersPost = (req, res, db) => 
      const customerId, orderStatus, productId, quantity, price = req.body;
      if (!customerId

      module.exports =
      handleOrdersPost: handleOrdersPost
      ;


      When I try to add multiple items using postman I get an error saying basically what I already know, that I need to repeat the id for the length of the array, because it works fine with just one item but I need it to work with multiple items.










      share|improve this question
















      I want to add an array of items with the same orderId, so I need to have the orderId repeated for the length of the array, how do I do that?



      My code:



      const handleOrdersPost = (req, res, db) => 
      const customerId, orderStatus, productId, quantity, price = req.body;
      if (!customerId

      module.exports =
      handleOrdersPost: handleOrdersPost
      ;


      When I try to add multiple items using postman I get an error saying basically what I already know, that I need to repeat the id for the length of the array, because it works fine with just one item but I need it to work with multiple items.







      javascript arrays json loops knex.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 28 at 22:56









      Jared Forth

      1,2073 gold badges8 silver badges21 bronze badges




      1,2073 gold badges8 silver badges21 bronze badges










      asked Mar 24 at 14:02









      ThorThor

      263 bronze badges




      263 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0
















          In one transaction, a bunch of insertion items' ids will and should be ordered, so you have to loop your array and count the orderId



          so your code may looks like:



          const array = [];
          db.transaction((trx) =>
          db.insert([])
          .into('orders')
          .transacting(trx)
          .then((ids) =>
          const orderItems = [];
          orderItems.forEach((item, index) =>
          item.orderId = ids[0] + index;
          );
          return db.insert(orderItems)
          .into('orderItem)
          .transacting(trx);
          )
          .then(trx.commit)
          .catch(trx.rollback);
          )





          share|improve this answer

























          • that didn't work

            – Thor
            Mar 25 at 10:40












          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/4.0/"u003ecc by-sa 4.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%2f55324585%2fhow-to-repeat-the-id-for-the-length-of-an-array-in-nodejs-and-knexjs%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0
















          In one transaction, a bunch of insertion items' ids will and should be ordered, so you have to loop your array and count the orderId



          so your code may looks like:



          const array = [];
          db.transaction((trx) =>
          db.insert([])
          .into('orders')
          .transacting(trx)
          .then((ids) =>
          const orderItems = [];
          orderItems.forEach((item, index) =>
          item.orderId = ids[0] + index;
          );
          return db.insert(orderItems)
          .into('orderItem)
          .transacting(trx);
          )
          .then(trx.commit)
          .catch(trx.rollback);
          )





          share|improve this answer

























          • that didn't work

            – Thor
            Mar 25 at 10:40















          0
















          In one transaction, a bunch of insertion items' ids will and should be ordered, so you have to loop your array and count the orderId



          so your code may looks like:



          const array = [];
          db.transaction((trx) =>
          db.insert([])
          .into('orders')
          .transacting(trx)
          .then((ids) =>
          const orderItems = [];
          orderItems.forEach((item, index) =>
          item.orderId = ids[0] + index;
          );
          return db.insert(orderItems)
          .into('orderItem)
          .transacting(trx);
          )
          .then(trx.commit)
          .catch(trx.rollback);
          )





          share|improve this answer

























          • that didn't work

            – Thor
            Mar 25 at 10:40













          0














          0










          0









          In one transaction, a bunch of insertion items' ids will and should be ordered, so you have to loop your array and count the orderId



          so your code may looks like:



          const array = [];
          db.transaction((trx) =>
          db.insert([])
          .into('orders')
          .transacting(trx)
          .then((ids) =>
          const orderItems = [];
          orderItems.forEach((item, index) =>
          item.orderId = ids[0] + index;
          );
          return db.insert(orderItems)
          .into('orderItem)
          .transacting(trx);
          )
          .then(trx.commit)
          .catch(trx.rollback);
          )





          share|improve this answer













          In one transaction, a bunch of insertion items' ids will and should be ordered, so you have to loop your array and count the orderId



          so your code may looks like:



          const array = [];
          db.transaction((trx) =>
          db.insert([])
          .into('orders')
          .transacting(trx)
          .then((ids) =>
          const orderItems = [];
          orderItems.forEach((item, index) =>
          item.orderId = ids[0] + index;
          );
          return db.insert(orderItems)
          .into('orderItem)
          .transacting(trx);
          )
          .then(trx.commit)
          .catch(trx.rollback);
          )






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 6:39









          SinuxSinux

          1,0602 gold badges8 silver badges23 bronze badges




          1,0602 gold badges8 silver badges23 bronze badges















          • that didn't work

            – Thor
            Mar 25 at 10:40

















          • that didn't work

            – Thor
            Mar 25 at 10:40
















          that didn't work

          – Thor
          Mar 25 at 10:40





          that didn't work

          – Thor
          Mar 25 at 10:40




















          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%2f55324585%2fhow-to-repeat-the-id-for-the-length-of-an-array-in-nodejs-and-knexjs%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

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript