Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Unexpected token ... #2681

Open
eliassal opened this issue Jan 1, 2024 · 1 comment
Open

SyntaxError: Unexpected token ... #2681

eliassal opened this issue Jan 1, 2024 · 1 comment
Labels

Comments

@eliassal
Copy link

eliassal commented Jan 1, 2024

Description

I have an application supposed to run in docker container (I am new to node apps) along with redis container and it uses the redis client. The code is

var express = require('express');
var app = express();
var redis = require('redis');

var client = redis.createClient(6379, 'redis');
client.on("error", function (err) {
    console.error("Redis error", err);
});

app.get('/', function (req, res) {
    res.redirect('/index.html');
});

app.get('/json', function (req, res) {
    client.hlen('wallet', function (err, coins) {
        client.get('hashes', function (err, hashes) {
            var now = Date.now() / 1000;
            res.json( {
                coins: coins,
                hashes: hashes,
                now: now
            });
        });
    });
});

app.use(express.static('files'));

var server = app.listen(80, function () {
console.log('WEBUI running on port 80');

but when I run both containers, redis is up and running but the one that has the node app exits with the error

TypeError: client.hlen is not a function
at C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\webui.js:15:12
at Layer.handle [as handle_request] (C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\node_modules\express\lib\router\layer.js:95:5)
at C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\node_modules\express\lib\router\index.js:346:12)
at next (C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\node_modules\express\lib\router\index.js:280:10)
at expressInit (C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (C:\Projects\Docker\Kubernetes\Container.Training\orchestration-workshop\dockercoins\webui\node_modules\express\lib\router\layer.js:95:5)

@eliassal eliassal added the Bug label Jan 1, 2024
@JatSh1804
Copy link

var express = require('express');
var app = express();
var redis = require('redis');

var client =async  ()=>{return await redis.createClient(6379, 'redis');}
client.on("error", function (err) {
    console.error("Redis error", err);
});
client.connect();

app.get('/', function (req, res) {
    res.redirect('/index.html');
});

app.get('/json', async function (req, res) {
   await client.hlen('wallet', function (err, coins) {
        client.get('hashes', function (err, hashes) {
            var now = Date.now() / 1000;
            res.json( {
                coins: coins,
                hashes: hashes,
                now: now
            });
        });
    });
});

You are accessing hlen before even initiating the client connection, need to use connect() to initate the connection.
You can refer #235 for accessing the result.
This should work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants