首页 > 解决方案 > 超出最大调用堆栈大小(node.js 中的 Web 套接字)

问题描述

我正在 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');
            });

标签: node.jsamazon-web-servicessocket.ioamazon-dynamodb

解决方案


您正在尝试发出存储在变量中的函数datos。由于该函数是异步的,因此您实际上并没有存储您在回调函数中获得的数据。你是 console.logging 它,但你没有将它存储在变量中。io.emit这就是失败的原因。我建议您阅读异步函数在 javascript 中的工作原理。在此处阅读有关许多新 javascript 开发人员遇到的这个问题的更多信息:如何从异步调用返回响应?


推荐阅读