I'm building a webapp with NodeJS and a MongoDB database. Currently the following is frustrating me - I am lacking in understanding the callback/(a)sync element and still learning this so I assume that it has something to do with this aspect.
I have two main modules:
DB.js export - this function works when called by itself:
DB.GetSuggestions: function(searchTerm) {
return MongoClient.connect(url).then(function(db) {
var collection = db.collection("AccentPairs");
collection.updateMany(
{Unstressed: searchTerm},
{$inc: {QueryCount: 1}}
);
var result = collection.aggregate([
{ $match: { Unstressed: searchTerm } },
{ $group: { _id: {"WordID": "$WordID", "WordName": "$WordName", "Unstressed": "$Unstressed", "Stressed": "$Stressed", "TranslationEn": "$TranslationEn"}, Forms: { $push: "$Field" }}}
]).sort( { Chosen: -1} ).toArray();
db.close();
return result;
});
}
TextHandler.js:
var DB = require("../DB");
function ImportText(Text) {
var fullText = Text.trim();
var TextObject = {AuthorName: "", Title: "", Words: []};
var currentWord = "";
var BOS = true;
var EOS = false;
let reg = /([а-яА-ЯЁё́]+)([^а-яА-ЯЁё́]*)/g;
var result;
while (result = reg.exec(fullText)) {
BOS = EOS;
EOS = (result[2].indexOf(".") > -1);
currentWord = result[1];
WordObject = {WordText: result[1], WordID: "0", Options: [], Tail: result[2], BOS: BOS, EOS: EOS};
TextObject.Words.push(WordObject);
}
//This is what doesn't work - GetSuggestions within the loop doesn't work.
TextObject.Words.forEach(function(wd){
console.log(wd.WordText);
DB.GetSuggestions(wd.WordText).then(function(suggestions){
wd.Options = suggestions;
});
});
}
I am trying to iterate over the TextObject.Words array and search the database for suggestions using the GetSuggestions function. Calling GetSuggestions within the loop doesn't work, outside of the loop it works.
Error message:
<--- Last few GCs --->
29335 ms: Mark-sweep 1386.5 (1440.6) -> 1386.5 (1440.6) MB, 1156.9 / 0.7 ms [allocation failure] [GC in old space req uested]. 30456 ms: Mark-sweep 1386.5 (1440.6) -> 1387.5 (1424.6) MB, 1120.6 / 0.7 ms [last resort gc]. 31576 ms: Mark-sweep 1387.5 (1424.6) -> 1388.4 (1424.6) MB, 1119.4 / 0.7 ms [last resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 000001186EBCFB49 1: /* anonymous /(aka / anonymous */) [C:\Home\CS\TextAnalysis\Ezhik\node_modules\mongodb\lib\url_parser.js:~7] [p c=000003CC6CBB279E] (this=000001186EB04381 ,url=0000004FD766F421 ) 2: arguments adaptor frame: 2->1 3: connect(aka connect) [C:\Home\CS\TextAnalysis\Ezhik\node_modules\mongodb\lib\mongo_client.js:~390] [pc=000...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
ImportTextcalled?