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

"this" is {} #154

Closed
limitall opened this issue Mar 8, 2019 · 2 comments
Closed

"this" is {} #154

limitall opened this issue Mar 8, 2019 · 2 comments

Comments

@limitall
Copy link

limitall commented Mar 8, 2019

schema.pre('save', next => {
console.log(this);
if (!this.metadata) {
this.metadata = {}
}
var now = new Date();

if (!this.metadata.createdAt) {
  this.metadata.createdAt = now
}
this.metadata.updatedAt = now
this.metadata.doc_type = this.modelName;
next()

})

with this code I am getting "this" is like empty object {}

@bojand
Copy link
Contributor

bojand commented Mar 8, 2019

Arrow functions / lambdas cannot work for schema functions (middleware, methods, etc...). Lambdas lexically bind this and cannot access the context. Use actual functions instead.

@meysamabl
Copy link

meysamabl commented May 10, 2019

Hi,
I am having the same issue with default, although I am using actual function. "this" returns {}.
Here is my sample code:
const lounge = require('lounge'); const userSchema = lounge.schema({ username: {type: String, key: true, generate: true}, surname: String, name: String, fullName: { type: String, readOnly: true, default: function (value) { console.log(this); return (this.name + ' ' + this.surname).trim() } }, lastSeen: {type: Date} }); const UserModel = lounge.model('User', userSchema); const myUser = new UserModel({username: 'test123',surname: 'Test', name: 'Test2'}); console.log(myUser);

this is the output I am getting:

{}
{ username: 'test123',
surname: 'Test',
name: 'Test2',
fullName: 'undefined undefined' }
(node:6602) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated

It also does not generate id attribute with UUID!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants