我有一个对象数组,我从一个文本文件解析,我正在尝试使用Node,Express,MongoDB以及前端的vanilla JS和JQuery发送到我的数据库。
当我发布时,MongoDB中显示的所有内容都是“_id”字段,“section”字段后跟一个空数组。我想我的问题是关于数据的结构,我已经玩了一下,但没有运气。
我的代码如下。任何帮助将不胜感激!
前端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模型:
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控制器:
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
}
答案 0 :(得分:1)
transferArray
数组总是为空,因为在发布之前之后才运行extractDetails
函数!