问题
Hi I'm developing a chat app and just want to know how do I fetch data from other fields of a table on mysqldatabase using sequelize because I'm totally new on this. Here is how I tried to do it:
io.on('connection', function(socket){
// Send all previously sent messages
for( i in messages ) {
socket.emit('chat message', messages[i]);
}
socket.on('chat message', function(msg){
console.log('message: ' + msg);
// Push the message into the in-memory array.
messages.push(msg);
// Storage the message for when the application is restarted.
sequelize.query('INSERT INTO chat_storage(chat) VALUES("'+msg+'")').success(function() {
// Insert was successful.
}).error(function (err) {
// Error inserting message
});
// Send the message to everyone
io.sockets.emit('chat message', msg);
});
});
function getStdout(command, args, fn) {
var childProcess = require('child_process').spawn(command, args);
var output = '';
childProcess.stdout.stdoutetEncoding('utf8');
childProcess.stdout.on('data', function(data) {
output += data;
});
childProcess.on('close', function() {
fn(output);
});
}
// Load Messages
sequelize.query('SELECT * FROM chat_storage ').success(function (rows) {
for( i in rows ) {
messages.push(rows[i].chat);
messages.push(rows[i].fname);
}
getStdout('php', ['message.php'], function(output) {
validator = output;
http.listen(3000, function(){
// Start server.
});
});
}).error(function (err) {
// Error!
});
now this script works but the problem is that both th field chat and fname loads on the same boat which is not what I want because the fname field should be loaded on a separate div
here is how I load them
<?php startblock('script') ?>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
var li = $(
'<div class="message user"><div class="message-info">'+
'<img width="40" height="40" src="temp/user/jessamine.jpg">' +
'<div class="message-timestamp">' +
' <time datetime="" class="timeago uk-text-small">09:22 am</time>' +
'</div></div><b></b>' +
' <div class="message-data">' +
' <p></p></div></div>');
// use the 'text' method to escape malicious user input
li.find('p').addClass('bubble').text(msg);
$('#message-list').append(li);
});
<?php endblock(); ?>
With this script the fname data should not be loaded but instead it is also loaded on the <p>.. which is I think its weird... Please help me how do I do it?
来源:https://stackoverflow.com/questions/25194153/sequelize-nodejs-how-do-i-fetch-data-from-other-fields-of-a-table