rethinkdb changesfeed in express socket.io 显示 [object object]
rethinkdb changesfeed in express with socket.io displaying [object object]
我正在使用 express.js、rethinkdb、thinky、socket.io。尝试将数据库中发生的任何更改通知索引页。所以我使用 rethinkdb changesfeed(我可以在 console.log 中看到更新)但是 cursor/feed 显示 object object 而不是数据。
当我将提要结果传递给 socket.emit 时,它显示 [object object]?
我阅读并实现了来自 rethinkdb 博客 (https://www.rethinkdb.com/blog/cats-of-instagram/) 的代码,但它不起作用。
请使用 socket.io header
向下滚动到底部
App.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var r = require('rethinkdb');
// *********** Thinky code*********************//
var changeStream = require("rethinkdb-change-stream");
var thinky = require('thinky')()
var type = thinky.type;
var r = thinky.r;
//thinky model
var User = thinky.createModel('User', {
author: type.string()
});
/*
User.changes().then(function (feed) {
feed.each(function (error, doc) {
if (error) {
console.log(error);
process.exit(1);
}
if (doc.isSaved() === false) {
console.log("The following document was deleted:");
console.log(JSON.stringify(doc));
}
else if (doc.getOldValue() == null) {
console.log("A new document was inserted:");
console.log(JSON.stringify(doc));
}
else {
console.log("A document was updated.");
console.log("Old value:");
console.log(JSON.stringify(doc.getOldValue()));
console.log("New value:");
console.log(JSON.stringify(doc));
}
});
}).error(function (error) {
console.log(error);
process.exit(1);
});
*/
//*********End Thinky Code ******************//
// Mongodb Example http://www.guru99.com/node-js-mongodb.html
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost/EmployeeDB';
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
//attach socket server to express server so it interacts with clients requests and responses
app.io = require('socket.io')();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
/* )))))))))))))))) socket.io Header ((((((((((((((((((( */
app.io.on('connection', function (socket) {
r.connect(host = 'localhost', port = 28015, db = 'test').then(function (conn) {
this.conn = conn;
return r.table("User").changes().run(this.conn).then(function (cursor) {
cursor.each(function (err, item) {
console.log(item);
//if (item && item.new_val)
socket.emit("message-from-server", { greeting: item });
});
})
});
// socket.emit("message-from-server", { greeting: "Say whatever here- it will show on the index page"});
});
module.exports = app;
来自您的代码:
console.log(item);
//if (item && item.new_val)
socket.emit("message-from-server", { greeting: item });
这告诉我 item
可能是一个对象,所以当您将它传递到网络 api 以添加到 DOM 时,它会呈现对象引用 [object Object]
因为它期待一个字符串(而不是一个对象)。因此,您可以使用对象的 .
语法或数组的 [<index>]
语法从 item
中提取您想要的信息,或者您可以执行类似 item.toString()
的操作来尝试将整个东西变成一个字符串。这实际上取决于 item
的结构以及如何将其添加到 DOM。您可以在客户端执行此操作,也可以在它从您的服务器发出之前执行此操作。
我正在使用 express.js、rethinkdb、thinky、socket.io。尝试将数据库中发生的任何更改通知索引页。所以我使用 rethinkdb changesfeed(我可以在 console.log 中看到更新)但是 cursor/feed 显示 object object 而不是数据。
当我将提要结果传递给 socket.emit 时,它显示 [object object]? 我阅读并实现了来自 rethinkdb 博客 (https://www.rethinkdb.com/blog/cats-of-instagram/) 的代码,但它不起作用。
请使用 socket.io header
向下滚动到底部App.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var r = require('rethinkdb');
// *********** Thinky code*********************//
var changeStream = require("rethinkdb-change-stream");
var thinky = require('thinky')()
var type = thinky.type;
var r = thinky.r;
//thinky model
var User = thinky.createModel('User', {
author: type.string()
});
/*
User.changes().then(function (feed) {
feed.each(function (error, doc) {
if (error) {
console.log(error);
process.exit(1);
}
if (doc.isSaved() === false) {
console.log("The following document was deleted:");
console.log(JSON.stringify(doc));
}
else if (doc.getOldValue() == null) {
console.log("A new document was inserted:");
console.log(JSON.stringify(doc));
}
else {
console.log("A document was updated.");
console.log("Old value:");
console.log(JSON.stringify(doc.getOldValue()));
console.log("New value:");
console.log(JSON.stringify(doc));
}
});
}).error(function (error) {
console.log(error);
process.exit(1);
});
*/
//*********End Thinky Code ******************//
// Mongodb Example http://www.guru99.com/node-js-mongodb.html
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost/EmployeeDB';
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
//attach socket server to express server so it interacts with clients requests and responses
app.io = require('socket.io')();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
/* )))))))))))))))) socket.io Header ((((((((((((((((((( */
app.io.on('connection', function (socket) {
r.connect(host = 'localhost', port = 28015, db = 'test').then(function (conn) {
this.conn = conn;
return r.table("User").changes().run(this.conn).then(function (cursor) {
cursor.each(function (err, item) {
console.log(item);
//if (item && item.new_val)
socket.emit("message-from-server", { greeting: item });
});
})
});
// socket.emit("message-from-server", { greeting: "Say whatever here- it will show on the index page"});
});
module.exports = app;
来自您的代码:
console.log(item);
//if (item && item.new_val)
socket.emit("message-from-server", { greeting: item });
这告诉我 item
可能是一个对象,所以当您将它传递到网络 api 以添加到 DOM 时,它会呈现对象引用 [object Object]
因为它期待一个字符串(而不是一个对象)。因此,您可以使用对象的 .
语法或数组的 [<index>]
语法从 item
中提取您想要的信息,或者您可以执行类似 item.toString()
的操作来尝试将整个东西变成一个字符串。这实际上取决于 item
的结构以及如何将其添加到 DOM。您可以在客户端执行此操作,也可以在它从您的服务器发出之前执行此操作。