我上传了一个带有collectionFS的csv文件,然后我需要解析这个文件。
我就是这样做的:
Template.fsbanks.events "change .myFileInput": (event, template) ->
FS.Utility.eachFile event, (file) ->
Fsbanks.insert file, (err, fileObj) ->
if err
Notifications.danger err
else
console.log "File saved"
console.log fileObj
Meteor.call "importTransactions", file, (error, result) ->
然后我想要一些流星方法:
Meteor.methods
importTransactions: (file) ->
reader = new FileReader
console.log file
reader.readAsText file
reader.onload = (event) ->
data = $.csv.toObjects(event.target.result)
console.log "File read #{data}"
_.map data, (transaction) ->
console.log transaction
t = {}
date = transaction["Value date"]
date_array = date.split("/")
new_date = date_array[2] + "-" + date_array[1] + "-" + date_array[0]
# console.log new_date
t["account_nbr"] = transaction["Account number"]
t["value_date"] = new Date new_date
t["amount"] = transaction["Amount in the currency of the account"].replace(/\s/g, '')
# console.log t["amount"]
t["description"] = transaction["Description"]
Meteor.call "createTransaction", t, t["Entry number"], (error, entry_nbr) ->
if error
console.log error
else
console.log id
createTransaction: (transaction, entry_nbr) ->
Transactions.upsert
entry_nbr: entry_nbr
,
$set: transaction
return entry_nbr
当我这样做时,collectionFS插入工作正常。
但是我遇到了insertTransaction服务器方法的问题:
Failed to execute 'readAsText' on 'FileReader': The argument is not a Blob.
如果我运行完全相同的代码客户端,它就可以工作。
运行代码服务器端应该更改什么?
答案 0 :(得分:0)
这是我结束的代码。它很实用,但我真的不知道为什么......
在我的模型中(共享客户端/服务器):
Meteor.methods
importTransactions: (file) ->
reader = new FileReader
console.log file
reader.readAsText file
reader.onload = (event) ->
data = $.csv.toObjects(event.target.result)
console.log "File read #{data}"
_.map data, (transaction) ->
console.log transaction
t = {}
selectValues = {}
date = transaction["Value date"]
date_array = date.split("/")
new_date = date_array[2] + "-" + date_array[1] + "-" + date_array[0]
# console.log new_date
selectValues["account_nbr"] = transaction["Account number"]
selectValues["value_date"] = new Date new_date
selectValues["entry_nbr"] = parseInt transaction["Entry Number"]
t["amount"] = transaction["Amount in the currency of the account"].replace(/\s/g, '')
# console.log t["amount"]
t["description"] = transaction["Description"]
Meteor.call "createTransaction", t, selectValues, (error, entry_nbr) ->
if error
console.log error
else
console.log id
在服务器上:
Meteor.methods
createTransaction: (values, upsertValues) ->
if upsertValues
console.log "upserting transaction"
console.log "transaction: #{values}"
console.log "Values: #{upsertValues["entry_nbr"]}"
Transactions.upsert upsertValues
,
$set: values
, (err, nbr) ->
if err
console.log err
else
console.log "upsert successful #{nbr}"
else
Transactions.insert values
, (err, nbr) ->
if err
console.log err
else
console.log "insert successful #{nbr}"