[英]Meteor.js and npm
So I'm using the Meteor package meteorhacks:npm in order to use npm packages from within my meteor code. 所以我使用Meteor包meteorhacks:npm以便从我的流星代码中使用npm包。 So I have a template:
所以我有一个模板:
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);
};
And this is the Meteor method that I'm calling: 这就是我正在调用的Meteor方法:
Meteor.methods({
saveFileinFolder: function (content){
var nodeFS = Meteor.npmRequire('node-fs');
var fs = new nodeFS({version:'0.1.7'});
console.log(content);
}
});
I'm trying to use the node package node-fs in order to save a file to a folder, but I keep getting the error : 我正在尝试使用节点包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
You are misunderstanding the way to use meteorhacks:npm. 您误解了使用meteorhacks:npm的方式。 You need to put the version number you want into a file called
packages.json
: 您需要将所需的版本号放入一个名为
packages.json
的文件中:
{ "node-fs": "0.1.7" } {“ node-fs”:“ 0.1.7”}
then restart meteor and it will install that node package for you. 然后重新启动流星,它将为您安装该节点软件包。 Then in your code, proceed as with a usual
require
, just use Meteor.npmRequire
instead. 然后在您的代码中,按照通常的
require
,只需使用Meteor.npmRequire
。 You will get the same object as from require
in node. 您将从节点中的
require
获得相同的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.