These are chat archives for Automattic/mongoose

2nd
Jul 2018
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:09 UTC
@lineus 👋
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:16 UTC

@ledlamp Virtuals are just getters/setters, which are assumed to be synchronous. You could try to use a method, something like:

worldSchema.methods.getCreatorName = async function (cb) {
  var acc =  await Account.findOne({id: this.creatorID}, cb);
  return acc.username;
}

or

worldSchema.methods.getCreatorName = async function (cb) {
  var acc =  Account.findOne({id: this.creatorID}, cb, function (err,userData) {
    //return user name here
  });
}
Kev
@lineus
Jul 02 2018 01:20 UTC
hey @JohnCdf
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:21 UTC
Thanks for checking out my issue, it's been bothering me for a bit now
Kev
@lineus
Jul 02 2018 01:21 UTC
no worries :smile:
don't use arrow functions for mongoose middleware, this won't work the way you need it to.
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:23 UTC
I didn't know that
Kev
@lineus
Jul 02 2018 01:23 UTC
@JohnCdf are you running express or koa or ?
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:24 UTC
express
Kev
@lineus
Jul 02 2018 01:24 UTC
do the logs show that you are hitting the route when it doesn't send a response back?
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:25 UTC
They do
Kev
@lineus
Jul 02 2018 01:26 UTC
can you share that particular route here or in a DM?
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:26 UTC
I can even do User.findOne({}) and it will give me a list of users
Sure thing
I don't use direct routes, though - I pass requests to my own router. Do you by any chance happen to use Visual studio code? We could do some live sharing on there
Kev
@lineus
Jul 02 2018 01:30 UTC
I would, but my OS isn't supported. I'm one version behind. I'm on OSX 10.11 which is probably why I don't see the option for live share in my app
I can't use docker either :frowning:
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:33 UTC

Ah no problem! Heres the jist of it

I tried to narrow down the code to the necessary pieces:
index.js

const routes = {
  '/api/users' : handlers.users
}
app.all('*', ( req, res ) => {
  handler = routes[pathname] ? routes[pathname] : handlers.notfound;

  let requestData = {};

  handler(requestData, function(someResponse){ // this is the callback
    res.send(someResponse)
  })

  })

handlers.users:

  //handlers.users will redirect to this function:
  handlers.users.get = function (data, callback) { // when we get to handlers.users, this is the handler called
    let email = typeof(data.headers.email) != 'undefined' && data.headers.email.trim().length > 0 ? data.headers.email : false;
    let password = typeof(data.headers.password) != 'undefined' && data.headers.password.trim().length > 4 ? data.headers.password : false;

  if (email && password) {
    User.findOne({ 'email': {"$regex" : email, "$options" : "i"} }, 'email username password bio posts', (err, userData) => {
    //here
  })
  }

})
And now I think I see a possible explanation hehe
Arrow function at query..
Kev
@lineus
Jul 02 2018 01:35 UTC
arrow functions as callbacks are fine in mongoose. it's just in middleware and getters/setters where they can be an issue ( anywhere that mongoose calls a method with a particular this value.
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:35 UTC
Oh, I see, I see
Kev
@lineus
Jul 02 2018 01:36 UTC
have you verified that the User.findOne() callback is being called?
is userData null when it gets called?
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:37 UTC
Yes - err is null and so is userData
When I dont specify a user (ie findOne({})) it sends all the users correctly
Kev
@lineus
Jul 02 2018 01:37 UTC
have you turned on mongoose debugging? it will let you see the query that gets sent to mongodb
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:38 UTC
I also tried that, no help
Let me try sending some logs
Kev
@lineus
Jul 02 2018 01:38 UTC
can you share the debug output from mongoose for that query?
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:39 UTC
Np
Here we go..
I tried with my email
Mongoose: users.findOne({ email: 'diaz.john312@gmail.com' }, { fields: {} })
Is the output
Huh
null
Kev
@lineus
Jul 02 2018 01:44 UTC
are you sure that you're hitting the correct route, if your mongoose query has '$regex' in it, so should the debug log
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:48 UTC
Ah yes
Here the correct log
Mongoose: users.findOne({ email: { '$regex': 'diaz.john312@gmail.com', '$options': 'i' } }, { fields: {} })
null
Now if I do
User.findOne({}, (e, firstUser) => {
  console.log(firstUser)
}))
There I am!
{ avatar: 
   { data: 
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 13,
        buffer: <Buffer 3c 42 69 6e 61 72 79 20 44 61 74 61 3e> },
     contentType: 'image/jpeg' },
  posts: [],
  _id: 5b3827b89bf65e3285a7dedc,
  email: 'diaz.312@gmail.com',
  username: 'JohnCdf',
  password: '$2b$10$8Lf0SMqbcvBPrxLKI.ouh.1/jXiS9nVP9RgoP7x2GP7.vrRm43MM.',
  bio: 'html hacker',
  __v: 0 }
Kev
@lineus
Jul 02 2018 01:51 UTC
the email fields don't match :smile:
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:51 UTC
O H
O H H
WHAT
OH
MY GOODNESS
Kev
@lineus
Jul 02 2018 01:52 UTC
sometimes it just takes a second pair of eyes!
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:52 UTC
Dude you saves my life! 😂
lineus @lineus has your hash
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:52 UTC
Thanks so much for that haha
Kev
@lineus
Jul 02 2018 01:52 UTC
anytime hehe
and just in time for bed! go go gadget tooth brush :smiley:
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:53 UTC
hehe, same here.
Goodnight my dude
Kev
@lineus
Jul 02 2018 01:53 UTC
Hope to see you here again @JohnCdf
Jonathan Díaz
@SKEPDIMI
Jul 02 2018 01:54 UTC
Let's stay in contact, no? I'll add you in git in case I miss something like that again haha
Renato Marinho
@renatomarinho
Jul 02 2018 03:35 UTC
This message was deleted
Apostolis A.
@Eksapsy_gitlab
Jul 02 2018 12:01 UTC

Hey! So ... I've got a little issue.
I want to call a method without having the initial instance. How do I get to do that?

Say I have a User model which has a verifyPassword method.
Well, I want something to function like this:

const User = model('users');

const user = User.find({ username });
user.verifyPassword(pass); // this doesn't work, how to use instead?
How can I do that without having the instance? I mean, after you first saved the document into the model, you no longer have access to the instance in another block of code. Have you?
Kev
@lineus
Jul 02 2018 12:04 UTC
@Eksapsy_gitlab you can make a static method on the model instead
Apostolis A.
@Eksapsy_gitlab
Jul 02 2018 12:04 UTC
Alright, just wanted to know if I'm doing something wrong. Thanks for the advice.
So, what's the use of the non-static methods?
I mean, in that case if I'm not missing something, they're useless after the User's instance has been deleted/missed
Kev
@lineus
Jul 02 2018 12:15 UTC
@Eksapsy_gitlab I suppose all of the properties on a model instance are useless after the instance is deleted :smile:
Kev
@lineus
Jul 02 2018 12:22 UTC
it really just depends on whether you want to call the method on the Model or on the instance you can do either
Lamp
@ledlamp
Jul 02 2018 23:18 UTC
@JohnCdf that's exactly what i did; it seemed a bit hacky but oh well
-
do virtuals overshadow real document values?
Lamp
@ledlamp
Jul 02 2018 23:29 UTC
oof no it doesn't
Error: Virtual path "name" conflicts with a real path in the schema
that sucks... i want to do user permissions where users have a can object with boolean properties so I can do stuff like user.can.deleteWorld, and i wanted these to be virtually-generated based on a role, unless the user document's can object explicity sets a value
hmm, maybe have that on a differently named field that the virtual can look up
yeah ill try that :D
Lamp
@ledlamp
Jul 02 2018 23:35 UTC
or maybe use a method instead of a virtual 🤔
if (user.can.doSomething) ...
if (user.can("doSomething")) ...
Lamp
@ledlamp
Jul 02 2018 23:52 UTC
hmm i think i'd need to be able to send all the permissions to the client so i guess a virtual getter is better