How do you set a current user profile on insert method?


#1

Hello,

I want to ask if how do you set a current user profile to an insert method?

My use case is to add the current user profile in the collection during importing of csv file, the normal insert process (not importing of csv) is the current user will fill-in the form then submit, during submission the profile of the user who created the document is added to that collection for filtering purposes using collection2 autovalue.

I’ve created this method for csv import:

Meteor.methods({
	'importFile':function(fileid,filename){
		// var csv = Meteor.require('CSV');
		var fs = Npm.require('fs');
		var path = Npm.require('path');
		var file = Uploads.find({_id:fileid});
		Meteor.setTimeout(function(){
			var filepath = path.join(process.env.PWD, '/imports/uploads-' + fileid + '-' + filename);
			CSV().from.stream(
				fs.createReadStream(filepath),
				{'escape':'\\'})
				.on('record',Meteor.bindEnvironment(function(row,index){
					Enrollments.insert({
						'fullName': row[0],
						'birthdate': new Date(row[1]),
						'ageOfEnrollee': row[2],
						'gender': row[3],
						'phone': row[4],
						'address': row[5],
						'city': row[6],
						'beneficiary': [
							{'name': row[7]},
							{'name': row[8]},
							{'name': row[9]},
					                ....array of beneficiary
						],
							'effectivityDate': new Date(row[17]),
							'maturityDate': new Date(row[18]),
						        'maritalStatus': row[19],
							'spouseName': row[20],
							'spouseDateOfBirth': row[21],
							'spouseAge': row[22],
						'children': [
							{'name': row[23]},
							{'birthdate': new Date(row[24])},
							{'age': row[25]},
							........array of children
						],
						'parent': [
							{'name': row[56]},
							{'dateOfBirth': new Date(row[57])},
							{'age': row[58]},
							{'name': row[59]},
							{'dateOfBirth': new Date(row[60])},
							{'age': row[61]}
						],
						'sibling': [
							{'name': row[62]},
							{'birthdate': new Date(row[63])},
							{'age': row[64]},
							......array of siblings
						],
						
					});
				}, function(error){
					console.log(error);
				}))
				.on('error',function(err){
					console.log(err);
				})
				.on('end',function(count){
				});
		},1000);
	}
});

After the import operation, the data is successfully added on the collection but there’s no users profile in it, so the filtering on client side is not working. The fields inside user profile is company, branch and insurer, i have to set this on every document.

i’ve used this autovalue snippet to grab users profile:

autoValue: function() {
        if (this.isInsert) {
          var user = Meteor.users.findOne({ "_id": this.userId }, { fields: { "profile": 1 } });
          if (user) {
            return user && user.profile.insurer;
          }
        } else {
          this.unset();  // Prevent user from supplying their own value
        }
      }

I tried to reuse the above code without the autovalue: and else statement like this:

'sibling': [
	{'name': row[62]},
	{'birthdate': new Date(row[63])},
	{'age': row[64]},
	......array of siblings
],
var user = Meteor.users.findOne({ "_id": this.userId }, { fields: { "profile": 1 } });
	if (user) {
	     return user && user.profile.company;
	} 

});
function (error){

}
but with no success, how do i successfully grab the user profile and store it in a collection during import?

Any help are greatly appreciated!

Thanks,