I am currently working on an express app and utilizing Knex as the query string builder.
During batch insert operations with an array of 1000+ objects, I encountered an error when the array exceeded a certain length. The specific error message is provided below in this question.
In my testing, I observed that if the testFields array contains 3 or fewer objects, the data gets inserted into the database successfully. However, any number greater than 3 triggers the mentioned error. This leads me to suspect that the issue lies in the array's size.
To investigate further, I isolated the problem by hardcoding the data set to be inserted. The variable testFields, which serves as input for the batchInsert method, can be found at the end of this post.
The database in use is hosted on Azure, and my node version stands at v12.18.0.
Here are the pertinent details from my package.json:
{
"name": "expressjs",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon ./bin/www"
},
...
}
Below, you will find the code snippets representing my Express route and Knex query logic.
const isolateProblem = (res) => {
// need to get connection string from vault
return getKnexWithConString
.then((knex) => {
return knex
.batchInsert("Tasks", testFields)
.returning("pk_Tasks")
.then((result) => {
res.send("okay");
return result;
})
.catch((err) => {
res.send("not okay 1");
console.log("err", err);
});
})
.catch((err) => {
res.send("not okay");
console.log("err2:", err);
});
};
router.get("/", async (req, res) => {
const data = await isolateProblem(res);
console.log("data", data);
});
The error I'm encountering occurs when the array surpasses 3 elements. It appears to be an unhandled promise warning originating from transaction.js:45:38, but I'm unsure about the root cause.
"(node:805) UnhandledPromiseRejectionWarning: TypeError: Cannot assign to read-only property 'originalError' of object 'TransactionError: Requests can only be made in the LoggedIn state, not the Final state' at ...node_modules/knex/lib/dialects/mssql/transaction.js:45:38 (node:805) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
This error originated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:805) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code."
The testFields array below illustrates the structure of the objects being processed. Note that for privacy reasons, sensitive field names and values have been obfuscated.
Given that this array has 4 elements, it triggers the aforementioned error. Removing one element from this array would allow the data insertion to proceed without issues.
Your assistance and insights on resolving this matter are greatly appreciated!
const testFields = [
{
SqlField1: "00000",
...
},
...
]