Skip to main content

One post tagged with "sails"

View All Tags

· One min read

1、创建时的回调

beforeValidate: fn(values, cb)
afterValidate: fn(values, cb)
beforeCreate: fn(values, cb)
afterCreate: fn(newlyInsertedRecord, cb)

2、修改的时候回调

beforeValidate: fn(valuesToUpdate, cb)
afterValidate: fn(valuesToUpdate, cb)
beforeUpdate: fn(valuesToUpdate, cb)
afterUpdate: fn(updatedRecord, cb)

3、销毁的时候回调

beforeDestroy: fn(criteria, cb)
afterDestroy: fn(destroyedRecords, cb)

如在创建用户的时候对密码机密操作:

var bcrypt = require('bcrypt');

module.exports = {
attributes: {
username: {
type: 'string',
required: true
},
password: {
type: 'string',
minLength: 6,
required: true,
columnName: 'encrypted_password'
}
},
beforeCreate: function (values, cb) {
bcrypt.hash(values.password, 10, function(err, hash) {
if(err) return cb(err);
values.password = hash;
cb();
});
}
};

· One min read

我在config/session中添加session配置的时候,提示我需要安装connect-redis

安装完后启动发现报错,如下信息:

Could not load Connect session adapter :: connect-redis
A hook (`session`) failed to load!

后来搜到github上的issue发现,还只能使用v1.4.5版本的,于是我切换到1.4.5的,启动就没报错了。

https://github.com/balderdashy/sails/issues/2379

· One min read

解决的思路主要还是使用原生的方式打开一个事务。

下面是在stackoverflow上的一个帖子的答案

http://stackoverflow.com/questions/25079408/how-to-handle-async-concurrent-requests-correctly/25100188#25100188

buyItem: function(req, res) {
try {
// Start the transaction
User.query("BEGIN", function(err) {
if (err) {throw new Error(err);}
// Find the user
User.findOne(req.param("userId").exec(function(err, user) {
if (err) {throw new Error(err);}
// Update the user balance
user.balance = user.balance - req.param("itemCost");
// Save the user
user.save(function(err) {
if (err) {throw new Error(err);}
// Commit the transaction
User.query("COMMIT", function(err) {
if (err) {throw new Error(err);}
// Display the updated user
res.json(user);
});
});
});
});
}
// If there are any problems, roll back the transaction
catch(e) {
User.query("ROLLBACK", function(err) {
// The rollback failed--Catastrophic error!
if (err) {return res.serverError(err);}
// Return the error that resulted in the rollback
return res.serverError(e);
});
}
}

不过提个建议就是,这种写法的风格不好,建议改成promise方式

还有个npm模块是在sails-mysql基础上封装了事务 https://github.com/postmanlabs/sails-mysql-transactions

· One min read

错误信息

以下是报的错误

error: Error: The hook `orm` is taking too long to load.
Make sure it is triggering its `initialize()` callback, or else set `sails.config.orm._hookTimeout to a higher value (currently 20000)
at tooLong [as _onTimeout] (C:\Users\KAMI\AppData\Roaming\npm\node_modules\sails\lib\app\private\loadHooks.js:92:21)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15

解决方案

在config目录下创建两个文件,orm和pubsub,不过据回答的作者说是名字并不重要。

// config/orm.js
module.exports.orm = {
_hookTimeout: 60000 // I used 60 seconds as my new timeout
};
// config/pubsub.js
module.exports.pubsub = {
_hookTimeout: 60000 // I used 60 seconds as my new timeout
};

不建议直接在 node_modules 中修改

原问题地址:http://stackoverflow.com/questions/28524926/the-hook-orm-taking-too-long-to-load