@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
});
}
this
won't work the way you need it to.
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
})
}
})
this
value.
User.findOne()
callback is being called?
userData
null when it gets called?
err
is null
and so is userData
findOne({})
) it sends all the users correctly
Mongoose: users.findOne({ email: 'diaz.john312@gmail.com' }, { fields: {} })
null
Mongoose: users.findOne({ email: { '$regex': 'diaz.john312@gmail.com', '$options': 'i' } }, { fields: {} })
null
User.findOne({}, (e, firstUser) => {
console.log(firstUser)
}))
{ 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 }
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?
Model
or on the instance you can do either
Error: Virtual path "name" conflicts with a real path in the schema
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
if (user.can.doSomething) ...
if (user.can("doSomething")) ...