超过最大调用堆栈大小(node.js中的Web套接字)

时间:2018-11-12 02:30:28

标签: node.js amazon-web-services socket.io amazon-dynamodb

我正在用nodejs实现一个Web套接字以对dynamodb进行查询,但出现此错误: buffer.js:398 Buffer.isBuffer = function isBuffer(b){                                    ^

RangeError: Maximum call stack size exceeded
    at Function.isBuffer (buffer.js:398:36)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:44:66)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)
    at hasBinary (E:\ProyectoSMET\Aprendiendo_nodejs_y_Angular\angular-socket-io-example-master\server\node_modules\has-binary2\index.js:58:59)

.....这是我的代码:

            let app = require('express')();
            let http = require('http').Server(app);
            let io = require('socket.io')(http);


            const AWS = require('aws-sdk');
            AWS.config.update({ region: 'us-west-2' });
            docClient = new AWS.DynamoDB.DocumentClient();
            const tableName = 'Dispositivo';

            var datos=docClient.scan({
                TableName: tableName
            },(err, data)=>{
                if(err) {
                    console.log(err);
                } else {
                    console.log(data);
                }
            });

            io.on('connection', (socket) => {

                // Log whenever a user connects
                console.log('user connected');

                // Log whenever a client disconnects from our websocket server
                socket.on('disconnect', function(){
                    console.log('user disconnected');
                });

                // When we receive a 'message' event from our client, print out
                // the contents of that message and then echo it back to our client
                // using `io.emit()`
                socket.on('message', (message) => {
                    console.log("Message Received: " + message);
                    io.emit('message', {type:'new-message', text: datos});    
                });
            });

            // Initialize our websocket server on port 5000
            http.listen(5000, () => {
                console.log('started on port 5000');
            });

1 个答案:

答案 0 :(得分:0)

您正在尝试发出存储在变量datos中的函数。由于该函数是异步的,因此您实际上并不存储在回调函数中获得的数据。您正在console.logging它,但没有将其存储在变量中。这就是io.emit失败的原因。我建议您阅读javascript中异步函数的工作方式。在此处详细了解许多新的javascript开发人员所遇到的问题:How do I return the response from an asynchronous call?