Creating user on the server with random username and no password


#1

I am very new to Meteor and trying to develop an online synchronous experiment. Generally, once the participants clicks the ‘AGREE’ button, I want to create a user and add that user into my Players collection.

My consent_page.js looks like this:

import './consent_page.html';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Template } from 'meteor/templating';
import { Meteor } from 'meteor/meteor'
import '../../../api/players/methods.js'


Template.consent_page.events({
    'submit .consent-form'(event) {
        event.preventDefault();
        //create a new player
        Meteor.call('players.addPlayer')
        //go to instructions
        FlowRouter.go('/instructions')   
    }
});

and my ‘players.addPlayer’ method looks like this

import { Meteor } from 'meteor/meteor';

import { Players } from './players.js';
import { Accounts } from 'meteor/useraccounts:core';
import { Random } from 'meteor/random'

Meteor.methods({
    'players.addPlayer'() {
        let username = Random.id();
        alert(username);
         user = Accounts.createUser({
             username: username,
         }, (err, res) => {
            if (err) {
                alert(err);
                alert(res);
            } else {
                // success!
                alert(user.userId());
            }
        });
        Players.insert({
            _id: this.userId,
            enterTime: new Date(),
            status: 'instructions',
            passedQuiz: false,
            quizAttempts: 0,
            needRematch: false,
            condition: 'control'
        });
    }
});

The collection Players.js has the definition of the collection.

export const Players = new Mongo.Collection('players');

However, this doesn’t work. I do get redirected to the instructions page, but the user doesn’t get created … I get the following error:

Exception while simulating the effect of invoking 'players.addPlayer' TypeError: Cannot read property 'createUser' of undefined
    at DDPCommon.MethodInvocation.players.addPlayer (methods.js:12)
    at livedata_connection.js:866
    at Meteor.EnvironmentVariable.EVp.withValue (meteor.js?hash=6d285d84547b3dad9717a7c89c664b61b45ea3d8:1090)
    at Connection.apply (livedata_connection.js:857)
    at Connection.call (livedata_connection.js:732)
    at Object.submit .consent-form (consent_page.js:12)
    at blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3775
    at Function.Template._withTemplateInstanceFunc (blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3744)
    at Blaze.View.<anonymous> (blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3774)
    at blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2617 TypeError: Cannot read property 'createUser' of undefined
    at DDPCommon.MethodInvocation.players.addPlayer (http://localhost:3000/app/app.js?hash=a0773a323a7194e8049d57c3e551a7ad4fe97cf8:264:24)
    at http://localhost:3000/packages/ddp-client.js?hash=14d966b7972bd95a1f7015fec9ac340f10508a44:4087:25
    at Meteor.EnvironmentVariable.EVp.withValue (http://localhost:3000/packages/meteor.js?hash=6d285d84547b3dad9717a7c89c664b61b45ea3d8:1090:15)
    at Connection.apply (http://localhost:3000/packages/ddp-client.js?hash=14d966b7972bd95a1f7015fec9ac340f10508a44:4078:60)
    at Connection.call (http://localhost:3000/packages/ddp-client.js?hash=14d966b7972bd95a1f7015fec9ac340f10508a44:3959:17)
    at Object.submit .consent-form (http://localhost:3000/app/app.js?hash=a0773a323a7194e8049d57c3e551a7ad4fe97cf8:142:16)
    at http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3775:20
    at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3744:12)
    at Blaze.View.<anonymous> (http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3774:25)
    at http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2617:30
meteor.js?hash=6d285d84547b3dad9717a7c89c664b61b45ea3d8:942 Error invoking Method 'players.addPlayer': Method 'players.addPlayer' not found [404]