【发布时间】:2014-09-13 20:20:48
【问题描述】:
我的sails.js 应用程序嵌入在php 项目中,其中php 为sails 模型生成了一些日期。在 beforeCreate 回调或某些自定义方法中找不到获取此日期的方法。我不想使用 db 来同步 2 个模型(来自sails 的模型和来自 php 的模型)。我需要向远程 php 应用程序发送一些日期。
sails.js v.0.9.x
这是我的控制器:
module.exports = {
index: function (req, res) {},
create: function (req, res) {
if ( !req.param('login') )
throw new Error('Login is undefined');
if ( !req.param('message') )
throw new Error('Initial message is undefined');
var user, thread;
User.create({
id: 1,
name: req.param('login'),
ip: req.ip
}).done( function (err, model) {
user = model;
if (err) {
return res.redirect('/500', err);
}
user.fetch(); // my custom method
});
return res.view({ thread: thread });
}
};
和型号:
module.exports = {
attributes: {
id: 'integer',
name: 'string',
ip: 'string',
fetch: function (url) {
var app = sails.express.app;
// suggest this but unlucky :)
app.get('/path/to/other/loc', function (req, res, next) {
console.log(req, res)
return next()
});
}
}
};
UPD 我的解决方案
型号:
beforeCreate: function (values, next) {
var options = 'http://localhost:1337/test',
get = require('http').get;
get(options, function (res) {
res.on('data', function (data) {
_.extend( values, JSON.parse( data.toString() ) );
next();
});
}).on('error', function () {
throw new Error('Unable to fetch remote data');
next();
});
}
【问题讨论】:
标签: sails.js