1

I have my files object on back-end:

[{ Id: 3,
  UserId: 7,
  FileType: 'application/pdf',
  FileContent:
   <Buffer 25 50 44 46 2d 31 2e 33 0d 0a 25 e2 e3 cf d3 0d 0a 0d 0a 31 20 30 20 6f 62 6a 0d 0a 3c 3c 0d 0a 2f 54 79 70 65 20 2f 43 61 74 61 6c 6f 67 0d 0a 2f 4f ... >,
  FileName: '1',
  UserUploadId: 7 },
...]

I am sending this object to the view:

res.render('dashboard/files/index',{'title': 'My files', 'my_files' : files})

On HTML I am rendering with handlebarsjs a table containing a row per file and a button that executes a function receiving as unique parameter the file Id

<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col">UserId</th>
            <th scope="col">View</th>
        </tr>
    </thead>
    <tbody>
        {{#each my_files}}
        <tr>
            <td>{{this.UserId}}</td>
            <td>
                <button onclick="viewPdf({{this.Id}})"></button>
            </td>
        </tr>
        {{/each}}
    </tbody>
</table>

In the same document, with JavaScript, I am trying to get the FileContent property of the object, but for this I need to find the file in the array my_files searching by Id parameter.

<script>
    function viewPdf(Id){
        var found_file = my_files.find(function(element) {
            return element.Id == Id;
        });

        console.log(found_file)
    }
</script>

But I am getting this error output:

Uncaught ReferenceError: my_files is not defined at viewPdf (......:465) at HTMLButtonElement.onclick

12
  • my_files seems not declared, try to fetch data in the function if (typeof my_files === "undefined") Commented Jul 30, 2019 at 7:09
  • typeof my_files -> "undefined" Commented Jul 30, 2019 at 7:12
  • Make a server call on the function after the if statement or declare my_files as global variable Commented Jul 30, 2019 at 7:14
  • The weird thing is: the table is being rendered with my_files information, but when I click the button and I try to find by Id on the array, my_files seems to be undefined Commented Jul 30, 2019 at 7:15
  • If the variable is scoped in a function or an object, you can't retrieve it just by "my_files". If you can find where my_files is stored, you could retrieve it after. If you don't see where my_files is, pass it in arguments function or make a server call ;) Commented Jul 30, 2019 at 7:26

1 Answer 1

2

So,

JavaScript running server and browser side is different. You can't find a declared server side variable in the browser, vice versa.

A way to shared variable between server and client :

function viewPdf(Id){
    $.ajax('/getFiles', {
        success:function(my_files){
            // put logic here
        }
    })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.