Passing a sql table created with alasql from Server (node.js) to Client (ejs)

292 views Asked by At

I would be very glad to get some help on the following topic, given I didn't manage to get through it. My objective is to gather some data on the server side (so far I'm using alasql to transform my CSV fil into a sql table), and then pass it as a parameter to the client side, to perform other sql operations on Client side (again with alasql). So far, I suceeded in

  • Transforming the CSV file into a sql table thanks to alasql
  • Passing a parameter from the Server side to the Client Side

But I did not manage to

  • Pass the sql table itself from the Server side to the Client side as a parameter

My best result in trying to do so is

`input=[object Object],[object Object],...`

instead of the sql table I would expect,which gives an error

My code on the Server side is the following:

var express = require('express');
var session = require('cookie-session'); // Charge le middleware de sessions
var bodyParser = require('body-parser'); // Charge le middleware de gestion     des paramètres
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var alasql=require('alasql');

var app = express();

var input=0;

alasql('SELECT * FROM CSV("public/data/output.csv",{separator:";"})',[],function(data){
    input=data;
    console.log(1)
});

/* On utilise les sessions */
app.use(session({secret: 'TBC'}))

/* S'il n'y a pas de todolist dans la session, on en crée une vide sous forme d'array avant la suite */
.use(function(req, res, next){
    next();
})

.use(express.static(__dirname + '/public'))

/* On affiche la todolist et le formulaire */
.get('/segmentation_clients', function(req, res) { 
    console.log(input);
    var Commercial='U.Morel';
    res.render('segmentation_clients.ejs', {Commercial:'U. Morel',input:input});
})

 .listen(8080);

The beginning of the JS code on the Client side (segmentation_clients.ejs):

var Commercial="<%=Commercial%>";
var input= <%=input%>;
console.log(input);

The output of the code (ok for the #Commercial parameter but not ok for the #input SQL table): enter image description here

I would greatly appreciate your help on this topic

Thanks a lot Stéphane

1

There are 1 answers

0
S. Le Brun On

I finally got the solution:

  • On Server side, I replacedinput:input

with

input:JSON.stringify(input)
  • On Client side, I replaced var input= <%=input%>;

with

var input= <%-input%>;

The first problem was a problem of format (solved by transforming the input in a JSON format), and the second problem was about evaluating the data inside the arry with "-" instead of only reading it "=", as far as I understood

Best Stéphane