Rethinkdb节点将提要更改为HTML

我试图显示从rethinkdb数据库到HTML(前端)的任何更改源,但我无法显示任何更改提要。

你能帮我解决这个问题吗?

该文件是app.js

var express = require('express'), app = express(), server = require('http').createServer(app), users = {}, db='testing', table = 'todos'; io = require('socket.io').listen(server); var bodyParser = require('body-parser'); server.listen(5000); app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); var config = require(__dirname + '/config.js'); var r = require('rethinkdbdash')(config.rethinkdb); io.sockets.on('connection', function(socket){ r.db(db).table(table).pluck('title').changes().run(). then(function(feed){ feed.each(function(err, item){ console.log(JSON.stringify(item, null, 2)); io.emit('new message', item); }); }); }); 

config.js文件

 module.exports = { rethinkdb: { host: '192.168.2.3', port: 28015, authKey: '', db: 'testing' }, express: { port: 5000 } }; 

index.html文件

 <html> <head> <title>Chat with socket.io and node.js</title> <style> #chat{ height:500px; } </style> </head> <body> <div id="chat"></div> <form id="send-message"> <input size="35" id="message"></input> <input type="submit"></input> </form> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script src="/socket.io/socket.io.js"></script> <script> jQuery(function($){ var socket = io.connect(); var $messageForm = $('#send-message'); var $messageBox = $('#message'); var $chat = $('#chat'); $messageForm.submit(function(e){ e.preventDefault(); socket.emit('send message', $messageBox.val()); $messageBox.val(''); }); socket.on('new message', function(data){ $chat.append(data + "<br/>"); }); </script> </body> </html> 

你在index.html上有一些语法错误,它会阻止客户端运行,那么主要问题是你在错误的领域采取行动。

这里是工作的代码:

app.js

 var express = require('express'), app = express(), server = require('http').createServer(app), users = {}, db='testing', table = 'todos'; io = require('socket.io').listen(server); var bodyParser = require('body-parser'); server.listen(5000); app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); var config = require(__dirname + '/config.js'); var r = require('rethinkdbdash')(config.rethinkdb); io.sockets.on('connection', function(socket){ r.db(db).table(table) .pluck('title') .changes() .run() .then(function(feed){ feed.each(function(err, item){ console.log(JSON.stringify(item, null, 2)); io.emit('new message', item.new_val.title); }) }) }) 

的index.html

 <html> <head> <title>Chat with socket.io and node.js</title> <style> #chat{ height:500px; } </style> </head> <body> <div id="chat"></div> <form id="send-message"> <input size="35" id="message"></input> <input type="submit"></input> </form> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script src="/socket.io/socket.io.js"></script> <script> jQuery(function($){ var socket = io.connect(); var $messageForm = $('#send-message'); var $messageBox = $('#message'); var $chat = $('#chat'); $messageForm.submit(function(e){ e.preventDefault(); socket.emit('send message', $messageBox.val()); $messageBox.val(''); }); socket.on('new message', function(data){ $chat.append(data + "<br/>"); }); }) </script> </body> </html> 

请注意,在变更Feed中,您将获得如下文档:

 { "new_val": { "id": "862e6410-f686-4a70-8a52-d4387181d4f2", "title": "12" }, "old_val": null } 

如果您返回整个文档,则客户端$chat.append(data + "<br/>")上的stringconcat可能会得到一个错误或一个'object Object'string,因为数据是一个对象,而不是一个string。

如果你只返回像这样的标题:

 io.emit('new message', item.new_val.title); 

然后stringconcat将运行正常。

另外,你的代码非常混乱。 我build议你一般会得到一些JavaScript的基础。