Syed Faizan
Syed Faizan

Reputation: 961

how to explicitly create a new user in KeyStone.js

I am trying to modify keyStone.js project to match my requirements,i am having problems adding a new user from the server side

var User = keystone.list('User');
User.add({
 name: {first:"Abcd",
 last:"xyz" },
 email: "[email protected]",
 password: "password",
 isAdmin: true
});
User.register();

But this isn't creating a new user in the MongoDB, any ideas what i maybe doing wrong?

Upvotes: 2

Views: 2563

Answers (1)

Rudi
Rudi

Reputation: 2995

You are mixing up schema definition for User and creation of a User

This could be your schema definition in models/User.js:

var User = keystone.list('User');

User.add({
    name: { type: Types.Name, required: true, initial: true  },
    email: { type: Types.Email, required: true, initial: true },
    password: { type: Types.Password, required: true, initial: true },
    isAdmin: { type: Boolean },
});

User.register();

Then you can create a user like this:

var User = keystone.list('User').model;

var user = new User({
    name: { first:'Abcd', last:'xyz' },
    email: '[email protected]',
    password: 'password',
    isAdmin: true
});

user.save(function (err) {
    if (err) {
        // handle error
        return console.log(err);
    }

    // user has been saved
    console.log(user);
});

Upvotes: 4

Related Questions