I have been looking for info to know how to connect Redis to Sails.js throw model - controller - view and I think I've got it.
My problem is that I think I don't understand the philosophy of Redis, its keys, etc.
I guess my Redis has not any keys, I mean it's not something like "name : Victor, surname : Garcia" but " Victor:García", wihtout a key, so I don't know how to set an attribute in the model of Redis.
So I've tried either no setting attributes or set just an attribute for the whole key (e.g. "Victor:Garcia:33:Seville:Spain") but without a result.
My model looks like this:
module.exports = {
schema: false,
connection: 'redis',
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
ta : 'string',
// Override toJSON instance method
toJSON: function() {
var obj = this.toObject();
return obj;
}
}
};
I tried this:
Redis.find()
.where({ ta: 'MB:400V:TRAF004:EP:MvMoment' })
.exec(function(err, users) {
// Do stuff here
console.log("User: " + users + " - " + err);
});
With no result, in fact, I get in console:
User: - null
No matter what I set in .where.
I also Tried to do something like this:
Model
module.exports = {
schema: false,
connection: 'redis',
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false
};
Without attributes, and I tried in the controller
Redis.find()
.where({ ta: 'MB:400V:TRAF004:EP:MvMoment' })
.exec(function(err, users) {
// Do stuff here
console.log("User: " + users + " - " + err);
});
Taking a look in google I've found this
http://sailsjs.org/#!/documentation/reference/waterline/models/stream.html
And I've tried to do the sample in my application.
Model (Redis.js):
module.exports = {
schema: false,
connection: 'redis',
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false
};
Controller:
estStream: function(req,res){
if (req.param('startStream') && req.isSocket){
var getSocket = req.socket;
// Start the stream. Pipe it to sockets.
Redis.stream().pipe(getSocket.emit);
} else {
res.view();
}
View:
<script type="text/javascript">
window.onload = function startListening(){
socket.on('gotUser',function(data){
console.log(data+' has joined the party');
});
};
</script>
<div class="addButton" onClick="socket.get('/monweb/testStream/',{startStream:true})">Stream all the Users !</div>
Bur when I click the button the result by console is as below:
error: Sending 500 ("Server Error") response:
TypeError: Object function (ev) {
if (ev == 'newListener') {
return this.$emit.apply(this, arguments);
}
var args = util.toArray(arguments).slice(1)
, lastArg = args[args.length - 1]
, packet = {
type: 'event'
, name: ev
};
if ('function' == typeof lastArg) {
packet.id = ++this.ackPackets;
packet.ack = lastArg.length ? 'data' : true;
this.acks[packet.id] = lastArg;
args = args.slice(0, args.length - 1);
}
packet.args = args;
return this.packet(packet);
} has no method 'on'
at Stream.pipe (stream.js:65:8)
at module.exports.testStream (/home/victor/gestamp-PRUEBAS/api/controllers/MonWebController.js:1364:24)
at routeTargetFnWrapper (/home/victor/gestamp-PRUEBAS/node_modules/sails/lib/router/bind.js:178:5)
at callbacks (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
at param (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
at param (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
at pass (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
at nextRoute (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
at callbacks (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
at /home/victor/gestamp-PRUEBAS/node_modules/sails/lib/router/bind.js:186:7
at alwaysAllow (/home/victor/gestamp-PRUEBAS/node_modules/sails/lib/hooks/policies/index.js:209:11)
at routeTargetFnWrapper (/home/victor/gestamp-PRUEBAS/node_modules/sails/lib/router/bind.js:178:5)
at callbacks (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
at param (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
at param (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
at pass (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/express/lib/router/index.js:145:5) [TypeError: Object function (ev) {
if (ev == 'newListener') {
return this.$emit.apply(this, arguments);
}
var args = util.toArray(arguments).slice(1)
, lastArg = args[args.length - 1]
, packet = {
type: 'event'
, name: ev
};
if ('function' == typeof lastArg) {
packet.id = ++this.ackPackets;
packet.ack = lastArg.length ? 'data' : true;
this.acks[packet.id] = lastArg;
args = args.slice(0, args.length - 1);
}
packet.args = args;
return this.packet(packet);
} has no method 'on']
events.js:74
throw TypeError('Uncaught, unspecified "error" event.');
^
TypeError: Uncaught, unspecified "error" event.
at TypeError (<anonymous>)
at emit (events.js:74:15)
at ModelStream.end (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/waterline/lib/waterline/utils/stream.js:61:10)
at module.exports.stream (/home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/waterline/lib/waterline/adapter/stream.js:25:66)
at /home/victor/gestamp-PRUEBAS/node_modules/sails/node_modules/waterline/lib/waterline/query/stream.js:42:20
at process._tickDomainCallback (node.js:492:13)
Sorry, I know I am new with this, I am sure that I am doing something (or a lots of things) wrong, could you help me? All what I want is stream data from Redis to the View. I would like to see in the view every change in Redis.
Thank you all.