1

I am trying to access Model component via sails.models.['CollectionName'] as these CollectionName will be dynamically sent into this piece of functionality. But it is throwing undefined error.

 /sails12.rc3/myapps/api/services/UserService.js:86
    var findQuery = sails.models['User'].find({id: inputID
                                               ^
 /sails12.rc3/myapps/api/services/UserService.js:86
    var findQuery = sails.config.models['User'].find({id: inputID
                                               ^
 /sails12.rc3/myapps/api/services/UserService.js:86
    var findQuery = sails.config.models['user'].find({id: inputID
                                               ^
 TypeError: Cannot read property 'find' of undefined
    at Object.UserService.formNewData   
       (/sails12.rc3/myapps/api/services/UserService.js:86:52)
at Object.bound [as formNewData] (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
at /sails12.rc3/myapps/api/services/UserInfoService.js:330:37
at fn (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:638:34)
at Immediate._onImmediate (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:554:34)
at processImmediate [as _immediateCallback] (timers.js:358:17)

Please note that sails.config is working perfectly fine..

I am using SailsJS 12 rc 3. ( I mean the latest Version at this time ).

Can you please suggest me about the troubleshooting in this regard.

Update 1: Hi Jason, In all 3 cases, the same errror. I could technically confirm that you are right on http://sailsjs.org/documentation/reference/configuration/sails-config-models

Still, I am not sure, if there are any update requires in

http://sailsjs.org/documentation/anatomy/my-app/config/models-js

currently in config/models.js,

module.exports.models = {
 connection: 'mongoDB',
 migrate: 'alter'
}

Please suggest if I need to update any config values in this models.js file?

Update 2

https://github.com/balderdashy/sails/blob/master/lib/hooks/orm/build-orm.js I could see the following values for these global values

sails.config.models  { connection: 'mongoDB', migrate: 'alter' }
sails.config.globals  { adapters: true, models: true, services: true }
sails.config.globals.models  true

And hence mine is not working..Please suggest some options.

Update 3

Thanks Travis. sails.models['user'].find is working fine without no change in the SailsJS version. So, let me test some more time.

Ps: Not sure, why i am unable to add a comment directly below ( MAC / chrome browser) . So, for now, editing this question itself.

Raj Rajen
  • 203
  • 2
  • 17
  • I hope the answer below helps. If you would like an additional resource, here is a chat room for sails.js, node.js, and waterline questions. https://gitter.im/balderdashy/sails – Travis Webb Aug 12 '15 at 02:57

2 Answers2

1

I think you're accessing it incorrectly.

Try: sails.config.models['User']

Jason Kulatunga
  • 5,814
  • 1
  • 26
  • 50
1

Try converting the model name to lowercase.

sails.models['CollectionName'] will not work, because within the models object, the collection names are all lowercase.

sails.model['collectionname'] should work.

I'm not sure why this is, but I think it will be something in the building of the ORM in waterline.

edit : I think this happens here