Search code examples
node.jsaws-sdk-nodejs

Node JS get text value not working quite right


When attempting to access the text content of an ID, I have tried 2 different ways -

var username = requests.query.username;
var username = $('#username').text();

The first attempt returned undefined, and the second attempt returned an empty string. Of course when I try and push this to my SQL server, it returns an error because it is a null value.

I thought about using JSDOM to insert the table into the html code and referencing the table values in the javascript, but I could not find any resources to do so.

Additionally, let it be known I am using NodeJS and that my HTML pages are separate from the JS file.


Solution

  • Is the code posted from your node app or frontend JS?

    Looking at the code its a bit unclear because you are using

    requests.query.username;
    

    then

    $('#username').text();
    

    Now assuming this was on the front end, the first variable of

    requests.query.username;
    

    Would have to be looking for a key of a nested object that would look like

    request = {query:{username:"usersname"}}
    

    And the other code block

    $('#username').text();
    

    Would appear to be using jQuery and trying to get the 'text' of the div with ID of username.

    So without more context its hard to say but, assuming this is on the nodejs side, then jquery will not work, there is no DOM for the selectors to work off of so that would never work (there are packages that can try and make this work but by default it wont, nor should you really need to do that)

    Now on the inverse, if you are doing this on the client, then, depending on the type of data you are trying to get [text, input value, innnerHTML] would dictate how what jquery call you make $("#username").text(), .html(), .val().

    With more context as to what you are doing would probably get you a better answer