3

I am new to nodejs. Trying to send some bootstrapped data (logged on user) from a nodejs server using the vash template back to the client with the index page. Need the bootstrapped data on the client to enable/disable certain options based on whether the user is logged in.

The server.js looks like this:

app.get("*", function(req, res) {
    res.render("index", {
        title: "Page title",
        bootstrappedUser: req.user
    });
});

index.vash

@html.extend('layout', function(model) {
    @html.block("scripts", function(model){
        @if(model.bootstrappedUser) {
        @{
            var a = model.bootstrappedUser;
        }
            <script type="text/javascript">
                window.bootstrappedUser = JSON.stringify(@a);
            </script>
        }
   })
})

But this results in an "UncaughtSyntaxError: Unexpected identifier " in the client html on the stringify function below:

    <script type="text/javascript">
            window.bootstrappedUser = JSON.stringify([object Object]);
    </script>

I tried to look up the Vash page on Github but there is virtually no documentation on script blocks using vash. Am I using the correct syntax inside 'index.vash' or is there some vash string interpolation that I am missing? Any help would be much appreciated.

Hemant
  • 41
  • 1
  • 5

1 Answers1

0

you must send data by JSON.stringfy(obj) from nodejs then

   <script>
       var test = @html.raw(@model)
   </script>

mean

server.js

app.get("*", function(req, res) {
    res.render("index", {
        title: "Page title",
        bootstrappedUser: JSON.stringfy(req.user)
    });
});

index.vash

@html.extend('layout', function(model) {
    @html.block("scripts", function(model){
        @if(model.bootstrappedUser) {
        @{
            var a = model.bootstrappedUser;
        }
            <script type="text/javascript">
                window.bootstrappedUser = @html.raw(a);
            </script>
        }
   })
})
S. Ferit Arslan
  • 199
  • 3
  • 8