Following sailsjs.org or github.com/balderdashy/waterline-docs. With this model:
const User = {
identity: 'user_tbl',
connection: 'postgres',
attributes: {
email: {
type: 'string',
required: true
},
secret: {
type: 'string',
required: true
},
toJSON: () => {
let user = this.toObject();
delete user.secret;
return user;
}
}
};
I tried [example one]:
module.exports = User;
/* # In main
import User = require('user');
waterline.loadCollection(Waterline.Collection.extend(User)) */
And [example two]:
module.exports = Waterline.Collection.extend(User);
/* # In main
import User = require('user');
waterline.loadCollection(User) */
With example one, Object.keys(this) === ['User'], and thus I get left with:
this.toObjectis not a function.
With example two I can't even get to the toJSON call, because this error occurs:
Cannot read property 'findOne' of undefinedwhen callingfindOne.
Without overloading toJSON everything works fine in example one.
I don't know a whole lot about ES6 but using var should work here. It's run in a separate context - when JSON.stringify is ran so not sure what let does or gives you there.
Just took another look, the issue was the type of function I'm using.
Swapping from => to function () fixed the issue. Seems that this is bound differently. Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Most helpful comment
Just took another look, the issue was the type of function I'm using.
Swapping from
=>tofunction ()fixed the issue. Seems thatthisis bound differently. Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions