meteor add harrison:papa-parse
meteor add themeteorchef:bert
meteor add reactive-var
meteor add fortawesome:fontawesome
$> meteor create csv
Then copy below 2 files into ./csv/
- csv.html
<head>
<title>cvs</title>
</head>
<body>
<h1>Welcome to CSV loader!</h1>
{{> upload}}
</body>
<template name="upload">
<h4 class="page-header">Upload a CSV, unfixed structure.</h4>
{{#unless uploading}}
<input type="file" name="upload_CSV">
{{else}}
<p><i class="fa fa-spin fa-refresh"></i> Uploading files...</p>
{{/unless}}
</template>
- csv.js
Order = new Mongo.Collection('order');
if (Meteor.isClient) {
Template.upload.onCreated( () => {
Template.instance().uploading = new ReactiveVar( false );
});
Template.upload.helpers({
uploading() {
return Template.instance().uploading.get();
}
});
Template.upload.events({
'change [name="upload_CSV"]' (event, template ) {
Papa.parse(event.target.files[0], {
header: true,
complete(results, file) {
Meteor.call('parseUpload', results.data, (error, response) => {
if (error) {
Bert.alert(error.reason, 'warning');
} else {
template.uploading.set(false);
Bert.alert('Upload complete!', 'success', 'growl-top-right' );
}
});
}
});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
Meteor.methods({
parseUpload(data){
//check(data, Array);
for ( let i = 0; i < data.length; i++) {
let item = data[i],
exists = Order.findOne({orderId: item.order_no});
if ( (item.order_no === undefined) || !exists ) {
Order.insert(item);
} else {
console.warn( 'Rejected. This item already exists.');
console.log(exists.Age + "," + item.order_no);
}
}
}
});
}
No comments:
Post a Comment