I could not create a collection and update user's profile

First I’m sorry if this make no sense as I am a very new to Meteor.

Well, In a collections folder, I have a Profile.js to handle profile for the collection. I am not sure i am doing this right or wrong, but I want to create a new collection call Profile, which for storing a user profile data outside of user collection. I checked on Mongo, but there is no profile collection have been created. Is there something wrong? That’s the first question.

Second: I want to either insert or update user profile as the sign up form was only required email and password, so user have nothing on their profile. To let user update their own profile on a new profile collection, so I am using condition to see if the user has already inserted or it will insert new fields to its collection, below is also the code i am using to check, please help me to correct them.

Thanks

Profile = new Mongo.Collection('profile');
Profile.allow({
insert: function(userId, doc){
	return !!userId;
},
update: function(userId, doc){
	return !!userId;
}
});

Profilechema = new SimpleSchema({
profileId: {
	type: String,
	label: "Profile ID",
	autoValue: function() {
		return this.userId
	},
	autoform: {
		type: "hidden"
	}
},
firstname: {
	type: String, 
	label: "First Name"
},
lastname: {
	type: String,
	label: "Last Name"
},
title: {
	type: String,
	label: "Title"
},
aboutyou: {
	type: String,
	label: "About you"
},

createdAt: {
	type: Date,
	label: "Created At",
	autoValue: function() {
		return new Date()
	},
	autoform: {
		type: "hidden"
	}
}
});

Profile.attachSchema( Profilechema );

Meteor.methods({

if (Profile.find({}).count() === 0) {
    insert_profile: function(firstname, lastname, title, aboutyou) {
    	check(firstname, String);
    	check(lastname, String);
    	check(title, String);
    	check(aboutyou, String);
    }

    Meteor.profile.insert(Meteor.userId(), {
    	firstname: firstname,
    	lastname: lastname,
    	title: title,
    	aboutyou: aboutyou

    });
  
} else {

	update_profile: function(firstname, lastname, title, aboutyou) {
        check(firstname, String);
        check(lastname, String);
        check(title, String);
        check(aboutyou, String);

    // ... further validation

	    Meteor.profile.update(Meteor.userId(), {
   		    firstname: firstname,
        	lastname: lastname,
        	title: title,
        	aboutyou: aboutyou   
    	});
	}
}
});

Hi

All collections are empty until there are no one documents are inserted

Second. You shouldn use allow/deny if all updates are made from server methods (see details).
Note that when new user is signing meteor call onCreateUser(func) where you should clear profile field and then store the data into Profile collection:

if(Meteor.isServer) {
    
    Accounts.onCreateUser(function (options, user) {
        user._id = user._id || new Mongo.ObjectID();

        // so here you can hook profile field passed from client
        let profileData = options.profile || {};
        profileData.userId = user._id;

        Profile.insert(profileData);

        user.profile = {};
        return user;
    });

    Profile._ensureIndex({userId: 1});
}

if(Meteor.isClient) {
    const userData = {
        email: 'take_all@your.money',
        password: '$$$$$$$$',
        name: 'Donald Trump',
        profile: {
            name: 'Donald Trump',
            hobby: 'hairdressing',
            age: undefined
        }
    }
    Accounts.createUser(userData)
}    

PS I’ve never used SimpleSchema, so I could miss something

Thanks for quick response @mrzafod! Well, I have a profile collection separated from useraccount system. I am not sure if I get you right, but your code seem to mix the profile collection inside the user collection (default useraccount system). I have told to avoid touching this account and by creating a new profile collection, where to store user profile fields instead. The thing was I couldn’t create a collection called “profile” and maybe because there is no documents were inserted since the first place.

It should come to my second question then, do you have any idea why the code didn’t work? as I have a condition to check if there is no documents found, then insert fields into the profile collection?

I hope I make sense to you!

Thanks
Somvannda

You should analyze the code again and see:

let profileData = options.profile || {};
profileData.userId = user._id;

Profile.insert(profileData);
user.profile = {};

I just grab profile field from default Accounts behaviour and insert the profile data into Profile collection, then simply clear profile for new user instance.
So when Accounts.onCreateUser is called its means that it is new user and you mau not check if it exists, so in my code I just insert profile in the Profile collection

Your code doesnt work because it is a mess! You should take a look at tutorials. Note that

Meteor.methods({
  if (Profile.find({}).count() === 0) {

Wont be work, even more I think it should throw an error on meteor start!