首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用socket.io显示[ object ]的rethinkdb变更提要

用socket.io显示[ object ]的rethinkdb变更提要
EN

Stack Overflow用户
提问于 2017-02-17 22:05:51
回答 1查看 114关注 0票数 1

我正在使用express.js,rethinkdb,thinky,socket.io。尝试将数据库中发生的任何更改通知索引页。所以我使用rethinkdb changesfeed (我可以在console.log中看到更新),但是游标/提要显示的是对象对象而不是数据。

当我将提要结果传递给socket.emit时,它会显示对象吗?我阅读并实现了来自rethinkdb博客(https://www.rethinkdb.com/blog/cats-of-instagram/)的代码,但它不起作用。

请用socket.io头向下滚动到底部

App.js

代码语言:javascript
复制
 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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-17 22:36:37

从你的代码:

代码语言:javascript
复制
console.log(item);
//if (item && item.new_val)
socket.emit("message-from-server", { greeting: item });

这告诉我,item可能是一个对象,所以当您将它传递给web以添加到DOM时,它会呈现对象引用[object Object],因为它需要一个字符串(而不是一个对象)。因此,您可以使用对象的item语法或数组的[<index>]语法从item.toString()中提取您想要的信息,或者您可以做一些类似于item.toString()的操作,尝试将整个事件转换为字符串。它实际上取决于item的结构以及如何将它添加到DOM中。您可以在客户端完成,也可以在服务器发出之前执行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42308203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档