0

I have an app running in Node.js with Express, and I wanted to dinamically change options on select object with jquery. This is actually not a big problem, but I'm having troubles on using the res.render parameters (which are mongoose documents) in the script tag. I use them without any trouble on the html (jade actually), but in the script tag I get a problem with the ObjectId not being a String.

This is an extract of the code: On the backend:

router.get("/new", function(req, res){
res.render("session/documentos/new", 
    {
        services: res.locals.services
    });
});

On the view

block content
div
    h1(class="text-center") New document
    form(id="newDoc" action="/session/documentos" method="POST")
        div(class="tab") Service:
            div(class="form-group") 
                select(class="form-control" name="svc" id="svc")
                    option(value="undefined" disabled selected) Choose one
                    for service in services
                        option(value=service._id)=service.name

script.
    $(document).ready(function() {
        var sessLenght = 0;
        var selectedSvc = #{services};
        $("#svc").change(function(){
            console.log("Service changed: " + selectedSvc);
        });
});

And this is the error I'm getting: Console error

And in Sources: Source error on ObjectId

So I'm being able to use with no troubles the "services" collection of documents, but when trying to use them on the script tag I'm getting problems with the ObjectId element.

I was thinking that one soution would be to convert to string the ObjectId when querying the database, but I think there might be a cleaner solution to that. Which might be the best way to solve the issue?

Any thoughts appreciated! Thanks in advance

Mahum
  • 1
  • Why have `selectedSvc` in the first place? The option's value is the `_id`, which is enough to find the service when the form is sent back to express. What is the point of your script? –  Nov 08 '18 at 14:51
  • 2
    Possible duplicate of [Passing objects to client in node + express + jade?](https://stackoverflow.com/questions/7681821/passing-objects-to-client-in-node-express-jade) –  Nov 08 '18 at 14:53
  • Hello Chris. Yes of course I could find the document in the backend with the id, the thing is that I want to populate the options inside a select html-object with the information inside the service document. I want to read the information inside the service document from the script to make changes in the view. I'll check the link, thx! – Mahum Nov 08 '18 at 16:31

1 Answers1

0

Try to change var selectedSvc = #{services};

to var selectedSvc = !{services};

or var selectedSvc = !{JSON.stringify(services)};

Asaf Aviv
  • 11,279
  • 1
  • 28
  • 45
  • Tried all those already, always the same issue. I "read" the document information but always an error over ObjectId element – Mahum Nov 08 '18 at 16:32
  • @Mahum Maybe try `services.toJSON()` or `services.toObject()` – Asaf Aviv Nov 08 '18 at 16:37
  • didn´t work either. Any other thoughts? Don't get it why I'm being able to use the documents in the html but not in the script tag. – Mahum Nov 09 '18 at 14:17