如何在流星中写入和读取csv或json文件

时间:2021-08-14 15:24:10

I am using papaParse and I want to save result of this package into file and users can download it. what is the best way to do this? also I am use this node.js code for do it

我正在使用papaParse,我想将此包的结果保存到文件中,用户可以下载它。做这个的最好方式是什么?我也使用这个node.js代码来做

var csv = Papa.unparse(Users.find().fetch());
console.log("csv : " + JSON.stringify(csv)); // get csv format (not in file)
fs.writeFile("meteorProject/public/", csv, function(err) {
if(err) {
    return console.log(err);
}

console.log("The file was saved!");
});

but give this error

但是给出这个错误

{ [Error: EISDIR: illegal operation on a directory, open 'meteorProject/public/'] I20160907-13:00:26.970(4.5)? errno: -21, I20160907-13:00:26.970(4.5)? code: 'EISDIR', I20160907-13:00:26.970(4.5)? syscall: 'open', I20160907-13:00:26.970(4.5)? path: 'meteorProject/public/' }

{[错误:EISDIR:目录上的非法操作,打开'meteorProject / public /'] I20160907-13:00:26.970(4.5)?错误:-21,I20160907-13:00:26.970(4.5)?代码:'EISDIR',I20160907-13:00:26.970(4.5)?系统调用:'打开',I20160907-13:00:26.970(4.5)?路径:'meteorProject / public /'}

and how can resolve it ?? thanks :-)

怎么解决呢?谢谢 :-)

2 个解决方案

#1


1  

This is the issue related to directory please check the directory path you are given here. i.e. - "meteorProject/public/". So what you need to change is just the directory name you are using . As i tried with my directory and its running well. Or i suggest you to try with the fileName.csv as well while saving the file like:- meteorProject/public/test.csv

这是与目录相关的问题请检查您在此处给出的目录路径。即 - “meteorProject / public /”。所以你需要改变的只是你正在使用的目录名。当我尝试使用我的目录并且运行良好时。或者我建议您在保存文件时尝试使用fileName.csv: - meteorProject / public / test.csv

or just completely change the path of directory like as i tried with my ubuntu machine and its running well.

或者只是完全改变目录的路径,就像我尝试使用我的ubuntu机器并运行良好一样。

var userArray = Users.find().fetch();
var data = Papa.unparse(userArray);
console.log("data is....");
console.log(data);
fs.writeFile('/home/parveen/test/test.csv',data,function(err,res){
    if(err){
        console.log("err while saving");
        console.log(err)
    }
    else{
        console.log("File saved");
        console.log(res);
    }
});

The above code save the file in the test folder via test.csv name.

上面的代码通过test.csv名称将文件保存在测试文件夹中。

Please check and let me know if you again facing any issue. If you want to know about your error in depth then please see the link:-

如果您再次面临任何问题,请检查并告诉我。如果您想深入了解您的错误,请参阅链接: -

https://github.com/bekk/grunt-retire/issues/2

https://github.com/bekk/grunt-retire/issues/2

Hope this would help!

希望这会有所帮助!

Thanks

谢谢

#2


1  

finally I used this package: Meteor-Files for solving this problem! this is sample code:

最后我用这个包:Meteor-Files来解决这个问题!这是示例代码:

 export(){
    var csv = Papa.unparse(Users.find({},{fields:{_id:0}}).fetch());
    Exports.write(csv,{
        fileName: 'backup'+ new Date().getTime() +'.csv',
        type: 'text/csv'
    }, function (error, fileRef) {
        if (error) {
            throw error;
        } else {
            console.log(fileRef.name + ' is successfully saved to FS. _id: ' + fileRef._id);
        }
    });
},

Exports is collection .This is the function that helped me.

出口是集合。这是帮助我的功能。

#1


1  

This is the issue related to directory please check the directory path you are given here. i.e. - "meteorProject/public/". So what you need to change is just the directory name you are using . As i tried with my directory and its running well. Or i suggest you to try with the fileName.csv as well while saving the file like:- meteorProject/public/test.csv

这是与目录相关的问题请检查您在此处给出的目录路径。即 - “meteorProject / public /”。所以你需要改变的只是你正在使用的目录名。当我尝试使用我的目录并且运行良好时。或者我建议您在保存文件时尝试使用fileName.csv: - meteorProject / public / test.csv

or just completely change the path of directory like as i tried with my ubuntu machine and its running well.

或者只是完全改变目录的路径,就像我尝试使用我的ubuntu机器并运行良好一样。

var userArray = Users.find().fetch();
var data = Papa.unparse(userArray);
console.log("data is....");
console.log(data);
fs.writeFile('/home/parveen/test/test.csv',data,function(err,res){
    if(err){
        console.log("err while saving");
        console.log(err)
    }
    else{
        console.log("File saved");
        console.log(res);
    }
});

The above code save the file in the test folder via test.csv name.

上面的代码通过test.csv名称将文件保存在测试文件夹中。

Please check and let me know if you again facing any issue. If you want to know about your error in depth then please see the link:-

如果您再次面临任何问题,请检查并告诉我。如果您想深入了解您的错误,请参阅链接: -

https://github.com/bekk/grunt-retire/issues/2

https://github.com/bekk/grunt-retire/issues/2

Hope this would help!

希望这会有所帮助!

Thanks

谢谢

#2


1  

finally I used this package: Meteor-Files for solving this problem! this is sample code:

最后我用这个包:Meteor-Files来解决这个问题!这是示例代码:

 export(){
    var csv = Papa.unparse(Users.find({},{fields:{_id:0}}).fetch());
    Exports.write(csv,{
        fileName: 'backup'+ new Date().getTime() +'.csv',
        type: 'text/csv'
    }, function (error, fileRef) {
        if (error) {
            throw error;
        } else {
            console.log(fileRef.name + ' is successfully saved to FS. _id: ' + fileRef._id);
        }
    });
},

Exports is collection .This is the function that helped me.

出口是集合。这是帮助我的功能。