3

I'm still learning to understand NodeJs/SailsJs, sorry if these sound too stupid but I can't seem to search google for a better explanation. I have tried searching for node OOP programming but doesn't seems to explain.

Taking the following query code

Company.findOne({'id':req.param('id')}).then(function(results){
    console.log(util.inspect(results, true, null))
})

The output will be

   { id: 1,
     companyName: 'Wunly Enterprise1',
     createdAt: null,
     updatedAt: Wed Jul 08 2015 01:43:19 GMT+0800 (SGT) }

But in SailJs, I can use the following code to update the record

 results.name = "change name";
 results.save(function(err,newresult){})

Howcome I am able to call save method when I console.log and there are no such method exists??

Chris Sim
  • 137
  • 1
  • 7

1 Answers1

6

console.log in NodeJs doesn't print inherited methods (like in chrome, ff...). save is attribute method that every record inherits.

If you want to print all properties of object, you can use this solution https://stackoverflow.com/a/8024294/1334743

just declare function

function getAllPropertyNames( obj ) {
    var props = [];

    do {
        props= props.concat(Object.getOwnPropertyNames( obj ));
    } while ( obj = Object.getPrototypeOf( obj ) );

    return props;
}

And implement it in your example

Company.findOne({'id':req.param('id')}).then(function(results){
    console.log(getAllPropertyNames(result));
})
Community
  • 1
  • 1
dkatavic
  • 196
  • 6