tarlepp / angular-sailsjs-boilerplate

'Boilerplate' for AngularJS + Sails.js
MIT License
307 stars 87 forks source link

Adding Custom methods #83

Closed johntom closed 9 years ago

johntom commented 9 years ago

Hi, I'm having an issue adding a custom route, something I routinely do when using my repo and I'm not sure why I get the referrence error "model[Book] is not defined" when it hits the route http://localhost:1337/booktest?token=eyJhbGciOiJIUzI1NiJ9.Mg._bgkwMqrKxFvVhupixHLIfvF5WpbihENyFpTQN5Eito

Thanks, John

If I add the following route 'GET /booktest': 'BookController.booktest' and method to BookController module.exports = .merge(.cloneDeep(require('../base/Controller')), { booktest: function(req, res) { console.log('books.........'); Book.find().exec(function (err, books) { if (err) return res.negotiate(err); return res.json(books);//{'data':todos}); }); } });

console

books.........
error: Sending 500 ("Server Error") response:
 ReferenceError: Book is not defined
    at Object.module.exports._.merge.booktest (C:\sails11\angular-sailsjs-boilerplate\backend\api\controllers\BookContro
ller.js:23:3)
    at bound (C:\sails11\angular-sailsjs-boilerplate\backend\node_modules\sails\node_modules\lodash\dist\lodash.js:729:2
1)
    at routeTargetFnWrapper (C:\sails11\angular-sailsjs-boilerplate\backend\node_modules\sails\lib\router\bind.js:179:5)

    at callbacks (C:\sails11\angular-sailsjs-boilerplate\backend\node_modules\sails\node_modules\express\lib\router\inde
x.js:164:37)
    at param (C:\sails11\angular-sailsjs-boilerplate\backend\node_modules\sails\node_modules\express\lib\router\index.js
:138:11)
    at pass (C:\sails11\angular-sailsjs-boilerplate\backend\node_modules\sails\node_modules\express\lib\router\index.js:
145:5)
    at nextRoute (C:\sails11\angular-sailsjs-boilerplate\backend\node_modules\sails\node_modules\express\lib\router\inde
x.js:100:7)
    at callbacks (C:\sails11\angular-sailsjs-boilerplate\backend\node_modules\sails\node_modules\express\lib\router\inde
x.js:167:11)
    at C:\sails11\angular-sailsjs-boilerplate\backend\node_modules\sails\lib\router\bind.js:187:7
    at verify (C:\sails11\angular-sailsjs-boilerplate\backend\api\policies\authenticated.js:37:14)
    at C:\sails11\angular-sailsjs-boilerplate\backend\node_modules\jsonwebtoken\index.js:65:18
    at process._tickDomainCallback (node.js:381:11) [ReferenceError: Book is not defined]
tarlepp commented 9 years ago

That is because I have disabled all other globals that sails itself. See backend/config/globals.js for more information.

So basically Book.find() => sails.models.book.find() should work.

johntom commented 9 years ago

Thanks again, I've spent the bulk of my sails time in version 0.10.5 so I'm a bit behind and will start reading more about 0.11. So either
sails.models.book.find().exec(function (err, books) { if (err) return res.negotiate(err); return res.json(books);//{'data':todos}); }); or chaning models:true in globals.js does the trick and Book.find().exec(function (err, books) {

tarlepp commented 9 years ago

Yep, i recommend that you disabled all but sails on those globals and then just use sails.models.foobar.find() or sails.services.yourservice.method()

This is basically sails related stuff nothing todo with my repo....