Skip to content

Commit

Permalink
Fix "Cannot read property 'seq' of null" error
Browse files Browse the repository at this point in the history
Collection drop commands were sent without waiting for their completion. The
userId counter insert could complete before the counter collection drop.
In that case the new counter would be deleted
  • Loading branch information
rcowsill committed Sep 24, 2020
1 parent 5b6a307 commit 21d5740
Showing 1 changed file with 68 additions and 50 deletions.
118 changes: 68 additions & 50 deletions artifacts/db-reset.js
Expand Up @@ -6,7 +6,6 @@
// before running it (default: development). ie:
// NODE_ENV=production node artifacts/db-reset.js

const _ = require("underscore");
const { MongoClient } = require("mongodb");
const { db } = require("../config/config");

Expand Down Expand Up @@ -37,7 +36,16 @@ const USERS_TO_INSERT = [
//"password" : "$2a$10$Tlx2cNv15M0Aia7wyItjsepeA8Y6PyBYaNdQqvpxkIUlcONf1ZHyq", // User2_123
}];

// Getting the global config taking in account he environment (proc)
const tryDropCollection = (db, name) => {
return new Promise((resolve, reject) => {
db.dropCollection(name, (err, data) => {
if (!err) {
console.log(`Dropped collection: ${name}`);
}
resolve(undefined);
});
});
}

const parseResponse = (err, res, comm) => {
if (err) {
Expand All @@ -58,60 +66,70 @@ MongoClient.connect(db, (err, db) => {
console.log(JSON.stringify(err));
process.exit(1);
}
console.log("Connected to the database: " + db);
console.log("Connected to the database");

const collectionNames = [
"users",
"allocations",
"contributions",
"memos",
"counters"
];

// remove existing data (if any), we don't want to look for errors here
db.dropCollection("users");
db.dropCollection("allocations");
db.dropCollection("contributions");
db.dropCollection("memos");
db.dropCollection("counters");

const usersCol = db.collection("users");
const allocationsCol = db.collection("allocations");
const countersCol = db.collection("counters");

// reset unique id counter
countersCol.insert({
_id: "userId",
seq: 3
});
console.log("Dropping existing collections");
const dropPromises = collectionNames.map((name) => tryDropCollection(db, name));

// Wait for all drops to finish (or fail) before continuing
Promise.all(dropPromises).then(() => {
const usersCol = db.collection("users");
const allocationsCol = db.collection("allocations");
const countersCol = db.collection("counters");

// reset unique id counter
countersCol.insert({
_id: "userId",
seq: 3
}, (err, data) => {
parseResponse(err, data, "countersCol.insert");
});

// insert admin and test users
console.log("Users to insert:");
USERS_TO_INSERT.forEach((user) => console.log(JSON.stringify(user)));

usersCol.insertMany(USERS_TO_INSERT, (err, data) => {
const finalAllocations = [];

// We can't continue if error here
if (err) {
console.log("ERROR: insertMany");
console.log(JSON.stringify(err));
process.exit(1);
}
parseResponse(err, data, "users.insertMany");

data.ops.forEach((user) => {
const stocks = Math.floor((Math.random() * 40) + 1);
const funds = Math.floor((Math.random() * 40) + 1);

finalAllocations.push({
userId: user._id,
stocks: stocks,
funds: funds,
bonds: 100 - (stocks + funds)
// insert admin and test users
console.log("Users to insert:");
USERS_TO_INSERT.forEach((user) => console.log(JSON.stringify(user)));

usersCol.insertMany(USERS_TO_INSERT, (err, data) => {
const finalAllocations = [];

// We can't continue if error here
if (err) {
console.log("ERROR: insertMany");
console.log(JSON.stringify(err));
process.exit(1);
}
parseResponse(err, data, "users.insertMany");

data.ops.forEach((user) => {
const stocks = Math.floor((Math.random() * 40) + 1);
const funds = Math.floor((Math.random() * 40) + 1);

finalAllocations.push({
userId: user._id,
stocks: stocks,
funds: funds,
bonds: 100 - (stocks + funds)
});
});
});

console.log("Allocations to insert:");
finalAllocations.forEach(allocation => console.log(JSON.stringify(allocation)));
console.log("Allocations to insert:");
finalAllocations.forEach(allocation => console.log(JSON.stringify(allocation)));

allocationsCol.insertMany(finalAllocations, (err, data) => {
parseResponse(err, data, "allocations.insertMany");
console.log("Database reset performed successfully")
process.exit(0);
});
allocationsCol.insertMany(finalAllocations, (err, data) => {
parseResponse(err, data, "allocations.insertMany");
console.log("Database reset performed successfully")
process.exit(0);
});

});
});
});

0 comments on commit 21d5740

Please sign in to comment.