Sails version: 1.0.2
Node version: 8.12.0
NPM version: 6.4.1
DB adapter name: sails-mysql
DB adapter version: N/A
Operating system: windows 10
Hello,
I am using sails version 1.0.2. It encounted an error when i add my toJson function in model. I have already searched to fix this issue. But i don't find any solution. And i am very new to nodejs and sailsjs. Here is the code:
module.exports = {
attributes: {
name:{
type: "string",
required: true
},
title:{
type: "string",
},
email:{
type: "string",
// email: true,
required: true,
unique: true
},
encryptedPassword:{
type: "string",
},
toJson: function(){
var obj = this.toObject();
delete obj.password;
delete obj.confirmation;
delete obj.encryptedPassword;
delete obj._csrf;
return obj;
}
}
};
In the toJson attribute of model users:
Model instance methods are no longer supported in Sails v1.
Please refactor the logic from this instance method into
a static method, model method or helper.
Hi @devkbsc! It looks like you missed a step or two when you created your issue. Please edit your comment (use the pencil icon at the top-right corner of the comment box) and fix the following:
As soon as those items are rectified, post a new comment (e.g. “Ok, fixed!”) below and we'll take a look. Thanks!
*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact [email protected]
The function I'm guessing you're trying to implement should be added as a property to the 'module.exports' object named 'customToJSON' (instead of 'toJson'). In you're code you're actually trying to define it in the same object as your attributes (/ columns). So you're code should probably look like this:
module.exports = {
attributes: {
name:{
type: "string",
required: true
},
title:{
type: "string",
},
email:{
type: "string",
//email: true,
required: true,
unique: true
},
encryptedPassword:{
type: "string",
}
},
customToJSON: function(){
var obj = this.toObject();
delete obj.password;
delete obj.confirmation;
delete obj.encryptedPassword;
delete obj._csrf;
return obj;
}
};
In the customToJSON attribute of model users:
Model instance methods are no longer supported in Sails v1.
Please refactor the logic from this instance method into
a static method, model method or helper.
still i am getting the same error! Did i miss to import any extentions? sorry i am new to sails! so i started my practice project.
Sorry to be a hassle, but it looks like your issue is still missing some required info. Please double-check your initial comment and try again.
*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact [email protected]
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- In the
customToJSONattribute of modelusers: Model instance methods are no longer supported in Sails v1. Please refactor the logic from this instance method into a static method, model method or helper. -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-still i am getting the same error! Did i miss to import any extentions? sorry i am new to sails! so i started my practice project.
The error makes it seem like you still define 'customToJSON' inside the 'attributes' of your model. Make sure you define it outside of the attributes.
module.exports = {
attributes: {
// ... your attributes
},
// Now it is outside of your attributes
customToJSON: function(){
var obj = this.toObject();
delete obj.password;
delete obj.confirmation;
delete obj.encryptedPassword;
delete obj._csrf;
return obj;
}
};
Hi @nino-vrijman thanks for helping out a fellow sailor!
@devkbsc Are you still experiencing this problem?
here I am using "1.1.0" and the problem still exist.
when I tried it, in console this is a plain object {} and this.toObject() is not a function.
I figured it out, it's because of using arrow functions. I think this could save some time to mention this in the docs.
@thg303: Thanks for the help, I was using arrow functions and it was causing all the troubles.
My working solution -
module.exports = {
attributes: {
...
},
customToJSON: function() {
return _.omit(this, ['password', 'confirmation', 'encryptedPassword', '_csrf']);
},
// This won't work -
// customToJSON: () => _.omit(this, ['password', 'confirmation', 'encryptedPassword', '_csrf']),
};
customToJSON is not even calling for me, created an issue - https://github.com/balderdashy/sails/issues/6865
Most helpful comment
@thg303: Thanks for the help, I was using arrow functions and it was causing all the troubles.
My working solution -
SO: Arrow functions and this