These are chat archives for Automattic/mongoose

18th
Aug 2018
Keo
@Keo_Dara012_twitter
Aug 18 2018 01:46
anyone know how to make enum default value?
Kev
@lineus
Aug 18 2018 01:47
@Keo_Dara012_twitter
role: {
  type: String,
  enum: ['admin', 'buy', 'seller' ],
  default: 'buy'
}
Keo
@Keo_Dara012_twitter
Aug 18 2018 01:48
oh thank
Kev
@lineus
Aug 18 2018 01:48
anytime :+1:
Snehanshu Phukon
@pSnehanshu
Aug 18 2018 04:20
How to make default function
Sorry
When accessing any property of this inside default function of a schema, its always undefined.
I guess this happens because the default functions are called before
I am able to set any other values.
Is there any way to trigger the default functions after some other fields are set? So that the default functions are able to derive data from other fields.
Kev
@lineus
Aug 18 2018 04:50
@pSnehanshu you might be able to do what you need to do in a pre validation hook
schema.pre('validation', function() {
  this is the document and it runs after the defaults but before pre-save, and save
})
if you need some help setting it up, just toss your schema in here and what you're trying to do. I'll take a look when I get up in the morning. :smile:
Snehanshu Phukon
@pSnehanshu
Aug 18 2018 05:04
good night
var Person = new Schema({
    name: String,
    email: String,
    avatar: {
         type: String,
         default: function(){ return 'https://www.gravatar.com/avatar/' 
 + md5(this.email) + '?d=retro' }
    }
})
This is a simplistic scenario.
Snehanshu Phukon
@pSnehanshu
Aug 18 2018 05:09
In this case, the avatar field should be automatically calculated from the email address. But as I mentioned, the this.email is undefined when the default function is called.
Keo
@Keo_Dara012_twitter
Aug 18 2018 07:43
image.png
i follow this
but it broken right here
so i need help
Kev
@lineus
Aug 18 2018 11:23
@pSnehanshu is avatar the first path in your actual schema? the defaults functions run in the order that the paths are declared in the schema.
For Example
@Keo_Dara012_twitter can you elaborate on what you mean by broken? Is it an error or just not working the way you expect?
Keo
@Keo_Dara012_twitter
Aug 18 2018 11:53
wait
i spend haft a day today
Keo
@Keo_Dara012_twitter
Aug 18 2018 11:59
it not error
it justs working the way i expected
Keo
@Keo_Dara012_twitter
Aug 18 2018 12:14
image.png
image.png
Kev
@lineus
Aug 18 2018 12:16
@Keo_Dara012_twitter you should be able to change any calls to update to either updateOne or updateMany accordingly.
Keo
@Keo_Dara012_twitter
Aug 18 2018 12:16
but it broken when change
Kev
@lineus
Aug 18 2018 12:17
is this the document in that code sample?
Keo
@Keo_Dara012_twitter
Aug 18 2018 12:19
maybe i confusing right?
Kev
@lineus
Aug 18 2018 12:22
it's kind of difficult to help from images of code in general, but it's more difficult when those images are only part of the code. You can add code samples here by surrounding it with lines that only have 3 back ticks like:
```
< code goes here>
```
but to answer your question, no I don't think you're confusing :)
Keo
@Keo_Dara012_twitter
Aug 18 2018 12:24
yeah i just get started coding
<UserSchema.methods.incLoginAttempts = function (cb) { // if we have a previous lock that has expired, restart at 1 if (this.lockUntil && this.lockUntil < Date.now()) { return this.updateOne( { $set: { loginAttempts: 1 }, $unset: { lockUntil: 1 } }, cb ) } // otherwise we're incrementing var updates = { $inc: { loginAttempts: 1 } } // lock the account if we've reached max attempts and it's not locked already if (this.loginAttempts + 1 >= MAX_LOGIN_ATTEMPTS && !this.isLocked) { updates.$set = { lockUntil: Date.now() + LOCK_TIME } } return this.update(updates, cb) }>
image.png
Keo
@Keo_Dara012_twitter
Aug 18 2018 12:29
or you can look in the blogpost
Kev
@lineus
Aug 18 2018 12:50
@keo, if you want to avoid that specific deprecation warning in your instance method you can do something like this:
Keo
@Keo_Dara012_twitter
Aug 18 2018 12:51
than you so much
Kev
@lineus
Aug 18 2018 12:52
honestly though, I would just silence the deprecation warnings while using mongoose @5.2.9 with node --no-deprecation
Keo
@Keo_Dara012_twitter
Aug 18 2018 12:54
so that warning is ok right?
Kev
@lineus
Aug 18 2018 12:59
yeah, your code isn't going to stop working because of the deprecations. You can watch Automattic/mongoose#6880 for updates.
Keo
@Keo_Dara012_twitter
Aug 18 2018 13:13
oh thank you
i might know nothing without you
Kev
@lineus
Aug 18 2018 13:49
I'm glad I could help @Keo_Dara012_twitter :)
Snehanshu Phukon
@pSnehanshu
Aug 18 2018 13:58
@lineus it seems that the defaults are set when one calls var person = new PersonModel.
Keo
@Keo_Dara012_twitter
Aug 18 2018 14:07
@lineus if you could help
help fix this code update to date
Kev
@lineus
Aug 18 2018 15:32
@Keo_Dara012_twitter do you mean to remove the deprecation warnings? or is there something else going on with it?
Keo
@Keo_Dara012_twitter
Aug 18 2018 15:33
Oh all the code doesn’t work
Keo
@Keo_Dara012_twitter
Aug 18 2018 16:12
The way it is
Arun Kumar
@arunkumar413
Aug 18 2018 17:35
Hi All
Does mongoose Model.findByIdAndUpdate() create a new entry if the id is empty?
Maurizio Bellemo
@mbellemo_twitter
Aug 18 2018 20:30

@lineus thanks for the suggestion with ensureIndexes, it worked great... Now I have another challenge.

router.post('/', (req, res) => {
    const userBody = _.pick(req.body, ['email', 'password']);
    const companyBody = _.pick(req.body, ['name']);

    const company = new Company(companyBody);
    var user = new User(userBody);

    // TODO - if user is not validated, company is still created
    company.save().then((company) => {
        return company;
    }).then((company) => {
        user.roles.push('admin');
        user.company_id = company._id;
        return User.ensureIndexes().then(_ => {
            return user.save().then(() => {
                return user.generateAuthToken();
            }).then((token) => {
                return res.header('x-auth', token).send(user);
            });
        });
    }).catch((e) => {
        res.status(400).send(e);
    });
});

I am trying to create a company object and a user object (which belongs to the company) at the same time. The problem is that if there a validation error the user is not created, but the company is (which is wrong). Which is the best approach here? Should I delete the company in the catch clause? How would you do it?

Kev
@lineus
Aug 18 2018 22:51
@arunkumar413 if you add the upsert property to the options object with a value of true it will.
@mbellemo_twitter do you know the version of the mongodb server you're using?