Meteor.js和npm

时间:2015-08-26 00:19:29

标签: javascript node.js meteor

所以我使用Meteor包meteorhacks:npm以便在我的流星代码中使用npm包。所以我有一个模板:

Template.upload.events({
    'submit form': function(event){
        var file1 = document.getElementById("file1").files;

        var file2 = $('[name=file1]').val();
        //console.log(file1[0].type);

        var reader = new FileReader();
        reader.onload = function(event) {
            var contents = event.target.result;
            Meteor.call("saveFileinFolder",contents);
            //console.log(contents);

        };

这就是我打电话的Meteor方法:

Meteor.methods({
    saveFileinFolder: function (content){
        var nodeFS = Meteor.npmRequire('node-fs');
        var fs = new nodeFS({version:'0.1.7'});
        console.log(content);
    }
});

我尝试使用节点包node-fs将文件保存到文件夹,但我一直收到错误:

I20150825-18:37:51.645(-4)? Exception while invoking method 'saveFileinFolder' TypeError: object is not a function
I20150825-18:37:51.646(-4)?     at [object Object].Meteor.methods.saveFileinFolder (app/server/home.js:4:15)
I20150825-18:37:51.646(-4)?     at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1)
I20150825-18:37:51.646(-4)?     at packages/ddp/livedata_server.js:648:1
I20150825-18:37:51.646(-4)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150825-18:37:51.646(-4)?     at packages/ddp/livedata_server.js:647:1
I20150825-18:37:51.646(-4)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150825-18:37:51.646(-4)?     at [object Object]._.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646:1)
I20150825-18:37:51.646(-4)?     at packages/ddp/livedata_server.js:546:1

1 个答案:

答案 0 :(得分:0)

你误解了使用meteorhacks的方式:npm。您需要将所需的版本号放入名为packages.json的文件中:

{“node-fs”:“0.1.7”}

然后重新启动meteor,它将为您安装该节点包。然后在您的代码中,按照常规require进行操作,只需使用Meteor.npmRequire即可。您将获得与节点中的require相同的对象。