sails.js

In sails.js, how to access session variables outside controller?

流过昼夜 提交于 2019-12-12 15:01:28
问题 In controller, it's easy. Accessing a session variables is straightforward: req.session.x = 1 However, how do I access that outside controller? Like in service? module.exports.test = function() { // No req.session here? }; 回答1: The session object in Sails isn't provided as a global; it's a property of the Request object. For server-side code that doesn't included the request as part of the scope (like services and models), you'll need to pass the request or the session along as an argument to

How to raise custom error message in Sails.js

前提是你 提交于 2019-12-12 14:18:40
问题 I am very new to Sails.js (0.9.13) and to node style programming in general. Any help appreciated. The collowing example is from a UserController: module.exports = { save: function (req, res) { var u = { firstName: req.param('firstName'), lastName: req.param('lastName'), email: req.param('email'), password: req.param('password'), }; User.create(u).done(function(err,user){ if(err) { if( err.code == 'ER_DUP_ENTRY' ) { return new Error('Duplicate: email address already in use.','UserController')

Sails.js - PATH variable - sails command not recognized

大兔子大兔子 提交于 2019-12-12 14:05:26
问题 After I npm installed Sails.js on Windows Server 2008, "sails" command is not recognized. Can someone give me a hint on what values to use in the PATH variable? As I understand it is Node.exe that runs the sails.js file. But if I try tunning "node sails.js" command in cmd, it recognizes it, but can't find some of the dependencies. On my Windows 7 machine everything installed and is running like a charm. 回答1: I ended up writing a batch file and putting it into system32 folder " c:\Windows

SailsJS best practice to seed database with data before other Models are initialized

谁说胖子不能爱 提交于 2019-12-12 12:08:17
问题 There is a model that all other models assume its existence. It should be initialized before any API function is called. The way I do this (it doesn't work): 1) Define model in api/models, let's call it Location.js 2) Add the following to bootstrap.js var Locations = require('../api/models/Locations.js'); module.exports.bootstrap = function (cb) { // seed the database with Locations var locationsObj = { country: 'Australia', states: ['Brisbane', 'Perth', 'Sydney'] }; Location.create

Soft delete in Sails/Waterline

纵然是瞬间 提交于 2019-12-12 11:16:48
问题 Trying to delete a user model using: //Hard Delete User.destroy({id:userId}, function(err, res){ //Hard Delete }) I need to do a soft delete on User model and currently setting a flag isDeleted to true on delete and updating document: updateUser.isDeleted = true; User.update({id:userId}, updateUser, function(err, res){ Update project }) and while fetching documents I am doing a check If isDeleted - true or not. Is there any In-built feature provided by Sails or Waterline which I can configure

Sails.js :remove bodyparser middleware for a specific route

巧了我就是萌 提交于 2019-12-12 10:15:07
问题 Is there any way to remove middleware for a particular route currently all the middlewares are listed in http.js file [ 'startRequestTimer', 'cookieParser', 'session', 'bodyParser', 'passportInit', 'passportSession', 'myRequestLogger', 'handleBodyParserError', 'compress', 'methodOverride', 'poweredBy', 'router', 'www', 'favicon', '404', '500' ] I want to remove the bodyParser middleware for a particular route Is it possible with sails.js?? 回答1: There's no mechanism for doing this

How to extract distinct values from a mongo database using Waterline and Sails.js (version 0.10)?

╄→гoц情女王★ 提交于 2019-12-12 09:58:42
问题 Using Sails.js version 0.10.x , assume I have a model Dog , populated as follows (writtenout in yaml format for convenience, but in my case it's actually in a mongo database.) dogs: - breed: "wolf" name: "Fido" - breed: "wolf" name: "Roger" - breed: "dingo" name: "Nipper" - breed: "dingo" name: "Ernie" - breed: "corgi" name: "Bernadi" - breed: "corgi" name: "Queenie" - breed: "poodle" name: "Christopher" - breed: "poodle" name: "Tony" etc So now I want to create a list of the available breeds

Restricting fields from being set in Sails.js Model

时光毁灭记忆、已成空白 提交于 2019-12-12 09:19:43
问题 So I have a model with some fields like so: // ... slug: { type: 'string', required: true, alphanumeric: true, minLength: 3, maxLength: 16 }, loggedinAt: 'date', // ... I'm using the Sails blueprint structure so it automatically maps everything. However, sometimes I have fields like loggedinAt which are strictly internal and I don't want them to be able to be set by the user. As it stands if I make a post requests with the loggedinAt field it will set it. How can I restrict this? 回答1: You can

Waterline default query condition

允我心安 提交于 2019-12-12 09:13:36
问题 How can I set the default where/sort condition in my SailsJS waterline model? In Rails I would use default scope. 回答1: Sails doesn't support default criteria on a per-model basis, but if you're using blueprint routes you can set default criteria for the route by overriding in your config/routes.js file, for example: "GET /user": { controller: 'user', action: 'find', where: {'deleted': false}, sort: 'age DESC' } This will work even if you don't have a find action defined in your UserController

Between Dates using Waterline ORM SailsJS

我只是一个虾纸丫 提交于 2019-12-12 08:54:48
问题 Goal: Return a list of items that were created between two dates. According to this issue https://github.com/balderdashy/waterline/issues/110 there is no between function just yet. However the work around is the following: User.find({ date: { '>': new Date('2/4/2014'), '<': new Date('2/7/2014') } }).exec(/* ... */); To be more exact, we don't want the hard coded dates above so we read in the input from a form submission like so: start = new Date(req.param('yearStart') + '/' + req.param(