Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I see a user I just created on MongoDB using "register" function inside "passport" module, within a NodeJS application? #11903

Closed
1 task done
Zohalmohal opened this issue Jun 6, 2022 · 2 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@Zohalmohal
Copy link

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.0.11

Node.js version

14.x

MongoDB version

5

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Issue

I am creating a new user by the following code:

try {
    User.register(new User({username : email , email : email , password : password}),  password, (err , user) => {
        if (err) {
            console.log("error happened!" , err);
            res.statusCode = 500;
            res.setHeader('Content-Type', 'application/json');
            res.json({ err: err });
        } else {
            console.log('success');
            passport.authenticate('jwt')(req, res, () => {
                res.statusCode = 200;
                res.setHeader('Content-Type', 'application/json');
                res.json({ success: true, status: 'Registration Successful' });
            });
        }
    });

But when I try:

console.log("the user is" , User);

I get this result:

the user is Model { User }

And by trying this:
console.log("the username is" , User.username);
I get:

the username is undefined

Also in the post man I can see this result:

{
"success": "New user function model(doc, fields, skipId) {\n model.hooks.execPreSync('createModel', doc);\n if (!(this
instanceof model)) {\n return new model(doc, fields, skipId);\n
}\n const discriminatorKey =
model.schema.options.discriminatorKey;\n\n if
(model.discriminators == null || doc == null || doc[discriminatorKey]
== null) {\n Model.call(this, doc, fields, skipId);\n return;\n }\n\n // If discriminator key is set, use the
discriminator instead (gh-7586)\n const Discriminator =
model.discriminators[doc[discriminatorKey]] ||\n
getDiscriminatorByValue(model.discriminators,
doc[discriminatorKey]);\n if (Discriminator != null) {\n
return new Discriminator(doc, fields, skipId);\n }\n\n //
Otherwise, just use the top-level model\n Model.call(this, doc,
fields, skipId);\n } created!" }

For this line of the code:

    res.status(201).json({ 'success': `New user ${User} created!` });

I don't know if an error happens while I am creating a new user and this result is because of that or it's normal responses and I must see the saved user somehow differen?

Also this is my user.js file if it helps:

const mongoose = require('mongoose');
var passportLocalMongoose = require('passport-local-mongoose');

const Schema = mongoose.Schema;

const userSchema = new Schema({
    email: {
        type: String,
        required: true,
        unique: true
    },
    username: {
        type: String,
        // required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    roles: {
        User: {
            type: Number,
            default: 2001
        },
        Editor: Number,
        Admin: Number
    },
    refreshToken: [String]
});

userSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model('User', userSchema);
@Zohalmohal Zohalmohal added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jun 6, 2022
@deistermatheus
Copy link

deistermatheus commented Jun 7, 2022

console.log("the user is" , User)

This is logging the model instance.

console.log("the username is" , User.username);

This is trying to access username of a model, not your user document, which is an instance of the User model (new User call).

I have not looked at the docs for the plugin, but from your callback structure, it seems that you receive a user instance after registering, which you could access by referencing the lower case user inside of the callback instead of the upper case User, which is the model.

(err, user) => {
console.log(User.username)
} // undefined, this is the constructor, does not have a username

(err, user) => {
console.log(user.username)
} // some string, is a document instance, has a username

@vkarpov15
Copy link
Collaborator

@deistermatheus is correct. Do console.log("the user is" , user);. In your example user is a user document, and User is a model class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

3 participants