I’m trying to insert a new collection into the database when a user creates their account for the first time, however, I’m getting an error. Completely stock on why this is happening.
Exception while invoking method 'createUser' TypeError: Cannot read property 'insert' of undefined
I’ve used similar code in the past and not had this problem.
Path: imports/startup/server/newUser.js
import { Meteor } from 'meteor/meteor';
import { Roles } from 'meteor/alanning:roles';
import { Accounts } from 'meteor/accounts-base';
import { Documents } from '../../api/documents/documents.js';
Accounts.onCreateUser((options, user) => {
if (options.profile) {
user.profile = options.profile;
}
Documents.insert({
title: "Test Title",
body: "Test Body"
})
return user;
});
Path: imports/api/documents/documents.js
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { Factory } from 'meteor/dburles:factory';
const Documents = new Mongo.Collection('Documents');
export default Documents;
Documents.allow({
insert: () => false,
update: () => false,
remove: () => false,
});
Documents.deny({
insert: () => true,
update: () => true,
remove: () => true,
});
Documents.schema = new SimpleSchema({
title: {
type: String,
label: 'The title of the document.',
},
body: {
type: String,
label: 'The body of the document.',
},
});
Documents.attachSchema(Documents.schema);
Do a console.log(“CREATING USER”); to make sure the event is firing, and where it’s firing. So put it first, see if it works. Then move it after options.profile, see if it fires there. Now we know that is OK.
Move if to after the Documents.insert. That should isolate it Documents.
I’m using this to insert:
Meteor.methods({
So I think your setup for adding posts to a collection is fundamentally incorrect.