0

I have an array of objects that I parsed from a text file that I'm trying to send to my database using Node, Express, MongoDB, and vanilla JS and JQuery on the frontend.

When I post, all that shows in MongoDB is the "_id" field and the "section" field followed by an empty array. I imagine my issue is with the structure of the data, and I've played around with that a bit, but had no luck.

My code is below. Any help would be much appreciated!

frontend JS:

var openFile = function(event) {
    var input = event.target;
    var reader = new FileReader();
  reader.onload = function() {
    var text = reader.result;
    // console.log(text.substring(0, 999999999999999));
      var section = text.substring(0, 9999999999999999);
      var subSection = {};
      var masterArray = new Object();
      var uploadDate = "";
      var period = "";
      var transferArray = [];
      var allData = [];
      var subSectionRegex = /   Total([\s\S]*?)Total|^\s+\d{4,5}([\s\S]*?)Total F/gm;
      var transferCodeRegex = /[0-9]{4,5}/;
      var voucherNumberRegex = /([0-9]{7,10}[\S])(?=\s+)|\s\d{5}\w{1}(?=\s)/g;
      var vendorRegex = /(?!\d{10})(\S+\s\S+(\s\S+)?)(?=\s+100)|(?!\d{10})(\S+(\s\S+)?)(?=\s+100)/gm;
      var descriptionRegex = /(?!\d{10})(\S+\s\S+(\s\S+)?)(?=\s+100)|(?!\d{10})(\S+(\s\S+)?)(?=\s+100)|(?!\d{10})(\S+\s(\s\S+)?)(?=\s+100)/g;
      var amountRegex = /(?:\s\w{3}\s+|Capitation\s+)(\d+,\d+.\d+)(?=")|(?:\s\w{3}\s+|Capitation\s+)(\d+,\d+,\d+.\d+)(?=")|(?:\s\w{3}\s+|Capitation\s+)(-\d+,\d+.\d+)(?=")|(?:\s\w{3}\s+|Capitation\s+)(-\d+,\d+,\d+.\d+)(?=")/gm;
      var oneLineAmountRegex = /(\d+,\d+,\d+.\d+)|\d+,\d+.\d+/g;
      var oneLineDescRegex = / - (\D+)|- \d+(\D+)/gm;

        subSection = section.match(subSectionRegex);
        subSection = subSection.filter(Boolean);

      function extractDate() {
        uploadDate = section.match(/Date (.*)/)[1].trim();
        uploadDate = new Date(uploadDate);
        allData["uploadDate"] = uploadDate;
      }
      extractDate();
      // console.log(allData.uploadDate);

      function extractPeriod() {
        period = section.match(/Period (.*)/)[1].trim();
        period = period.split(" ");
        period = period[0];
        period = parseInt(period);
        // console.log("period: " + period);
        allData["period"] = period;
      }
      extractPeriod();
      // console.log(allData.period);

      function extractDetails() {
        for(var i = 0; i < subSection.length; i++) {
            if(subSection[i].match(transferCodeRegex) && subSection[i].match(voucherNumberRegex) && subSection[i].match(vendorRegex) && subSection[i].match(descriptionRegex) && subSection[i].match(amountRegex)) {
                transferArray.push({
                    "transferCode": subSection[i].match(transferCodeRegex),
                    "details": [{
                        "voucherNumber": subSection[i].match(voucherNumberRegex),
                        "vendor": subSection[i].match(vendorRegex),
                        "description": subSection[i].match(descriptionRegex),
                        "amount": subSection[i].match(amountRegex)
                    }]
                })
            } else {
                transferArray.push({
                    "transferCode": subSection[i].match(transferCodeRegex),
                    "details": [{
                        "voucherNumber": subSection[i].match(voucherNumberRegex),
                        "description": subSection[i].match(oneLineDescRegex),
                        "amount": subSection[i].match(oneLineAmountRegex)
                    }]
                })
            }
        }
        var clean = transferArray.map(function(transfer){
            transfer.details = transfer.details.map(function(detail){
                detail.amount = detail.amount.filter(function(quantity){
                if(quantity !== null){
                    return true;
                }
                });
                detail.amount = detail.amount.map(function(quantity){
                var num = quantity.replace(/[^-\d\.]+/g, '');
                    return Number(num);
                });
                return detail;
            });
            return transfer;
            });
      }
        //adds detailed data to allData array
      allData["section"] = transferArray;

      $.ajax({
        url: "http://localhost:3000/transfers/api",
        type: "POST",
        data: allData,
        datatype: 'json',
        success: function(data, textStatus, jqXHR) {
            alert('posted!')
        },
        error: function(data, textStatus, errorThrown) {
            alert('failed')
        }
      })
      extractDetails();
      console.log(allData);

      function sendDataToDB() {

      }
    }
  reader.readAsText(input.files[0]);
};

api model:

var mongoose = require('mongoose');

var TransferSchema = new mongoose.Schema({
    uploadDate: Date,
    period: Number,
    IFMISFile: { type: mongoose.Schema.ObjectId, ref: 'ifmisFile' },
    section: [{
        transferCode: String,
        type: String,
        details: [{
            transferRecipient: String,
            voucherNumber: String,
            vendor: String,
            description: String,
            amount: Number
        }] 
    }] 
});

//convert schema to model
var Transfer = mongoose.model('Transfer', TransferSchema); 

//export model for use elsewhere in app
module.exports = Transfer;

api controller:

var Transfer = require('../models/Transfer');

//Get all transfers
function getAll(request, response) {
    Transfer.find(function(error, transfers) {
        if(error) response.json({message: 'couldn\'t find any transfers'});

        response.json({ transfers: transfers });
    }).select('-__v');
}

//Post a new transfer
function createTransfer(request, response) {
    console.log('posting');
    console.log('body: ' + request.body);

    var transfer = new Transfer(request.body);

    transfer.save(function(error) {
        if(error) response.json({ message: 'could not create transfer because ' + error });

        response.json({ transfer: transfer });
    });
}

//Delete
function removeTransfer(request, response) {
  var id = request.params.id;

  Transfer.remove({_id: id}, function(error) {
    if(error) response.json({message: 'Could not delete transfer b/c:' + error});

    response.json({message: 'Transfer successfully deleted'});
  }).select('-__v');
}

module.exports = {
    getAll: getAll,
    createTransfer: createTransfer,
    removeTransfer: removeTransfer
}
6
  • what does it show in the createTransfer function i.e console.log('posting');console.log('body: ' + request.body); Commented Dec 28, 2015 at 11:44
  • Hey, sorry for the delay, I was off the grid with family. The createTransfer function logs [object Object]. In my DB I get the following: {"_id": xxx, "section": [], "_v": xxx}, whereas "section" is empty and none of the other keys (period, date) pass through. Commented Jan 5, 2016 at 4:11
  • i suspect that the data you sent from front-end to back-end does not match your Transfer schema. Try inspect the request.body by console.log('body': + JSON.stringify(request.body)) and also the transfer object Commented Jan 5, 2016 at 5:39
  • Hmmmm, when I log JSON.stringify... it returns body: {}. I've logged the transfer object from the frontend and it returns everything as it should be, with the date format as you'd see if you logged new Date(1/1/2016) Commented Jan 5, 2016 at 13:51
  • try move the $.ajax call after this extractDetails();console.log(allData); and see if it works Commented Jan 5, 2016 at 17:12

1 Answer 1

2

The transferArray array is always going to be empty because you aren't running the extractDetails function until after you are posting!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.