Error when creating a new user account

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);

Documents is not defined.

I think your path is wrong.

import { Documents } from ‘…/…/api/documents/documents.js’;

Try adding console.log(“YOU FOUND ME!”); to that file and check your console.

That should point you in the right direction. Welcome to Meteor!

Path appears to be right. You found me appears in terminal

path has been fixed. still not working

Hrmm. Okay that’s part 1.

Documents.insert({
title: “Test Title”,
body: “Test Body”
})

Should have a semi colon on it. });

That could be a reason.

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.

Documents.allow({
  insert: () => false,
  update: () => false,
  remove: () => false,
});

I think this needs to be wrapped up properly. Meteor doesn’t really know what this is, which explains the undefined part of the error. Perhaps.

Hrmmm. No. I’m just doing things different.

I’ve got some code that does this:

var post_id = Posts.insert({

I’m also not using a schema at all, you might have an incorrect config there.

Hope any of this helps you out. Let me know what you discover.

Since you used export default Documents shouldn’t the import be

import Documents from '../../api/documents/documents.js';

that works when I reproduce this

1 Like

Jeepers. There’s a day I won’t get back.

What is the difference between import { Documents} and import Documents

I had to look it up myself here

2 Likes