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;
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
add a comment
|
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
add a comment
|
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
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
javascript arrays json loops knex.js
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
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
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);
)
that didn't work
– Thor
Mar 25 at 10:40
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
)
that didn't work
– Thor
Mar 25 at 10:40
add a comment
|
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);
)
that didn't work
– Thor
Mar 25 at 10:40
add a comment
|
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);
)
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);
)
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
add a comment
|
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
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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