可以通过以下步骤完成:
meteor add aldeed:collection2
Users = new Mongo.Collection('users');
UserSchema = new SimpleSchema({
name: {
type: String,
label: "Name"
},
email: {
type: String,
label: "Email"
},
// 其他字段...
});
Users.attachSchema(UserSchema);
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { JsonToCSV } from 'meteor/udondan:json2csv';
export const exportUsersToCSV = new ValidatedMethod({
name: 'users.exportToCSV',
validate: null,
run() {
const data = Users.find().fetch();
const csv = JsonToCSV(data, true);
return csv;
},
});
import { Meteor } from 'meteor/meteor';
import { Picker } from 'meteor/meteorhacks:picker';
import { exportUsersToCSV } from './methods/exportUsersToCSV';
Picker.route('/export-users', (params, req, res) => {
const csv = exportUsersToCSV.call();
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename=users.csv');
res.end(csv);
});
这样,你就成功地将用户数据从Meteor导出到.CSV文件了。你可以根据实际需求对以上代码进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云