Bitcoin Forum
May 09, 2024, 06:48:54 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Node.js help needed  (Read 89 times)
Coinoplex (OP)
Copper Member
Sr. Member
****
Offline Offline

Activity: 250
Merit: 412


Building A Crypto Empire


View Profile
November 23, 2021, 02:19:52 PM
 #1

Hello guys,

The developer I work with have been stuck with this issue for few days now and I need help from someone experienced in js to answer this. I have of course changed the names of the things so it doesn’t compromise any security in the platform, code is very similar tho.


sample.js code thats causing the issue:

const getColourById = async (req, res) => {
  let id = req.query["id"]
  let strength = null;
  let colour = null;
  client.query(`SELECT * FROM colours_strength_table WHERE id == ${id}`,
        function(err, result) {
            for (let r in rows) {
                if (r["id"] === id) {
                    strength = r["strength"]
                    colour = r["colour"]
                }
            }
        })
  if (strength === null && colour === null) { return res.json("success": false)}
  return res.json({
    outputs: [
        { strength: strength, colour: colour}
    ]
  })
}

Issue being raised in Terminal:

sample.js:518
            for (let r in rows) {
                          ^

ReferenceError: rows is not defined
    at Query.callback (/home/dev/project/node/lib/apps/sample.js:518:16)
    at Query.handleError (/home/dev/project/node/node_modules/pg/lib/query.js:128:19)
    at Client._handleErrorMessage (/home/dev/project/node/node_modules/pg/lib/client.js:335:17)
    at Connection.emit (events.js:314:20)
    at /home/dev/project/node/node_modules/pg/lib/connection.js:114:12
    at Parser.parse (/home/dev/project/node/node_modules/pg-protocol/dist/parser.js:40:17)
    at Socket.<anonymous> (/home/dev/project/node/node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.


"You Asked For Change, We Gave You Coins" -- casascius
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715280534
Hero Member
*
Offline Offline

Posts: 1715280534

View Profile Personal Message (Offline)

Ignore
1715280534
Reply with quote  #2

1715280534
Report to moderator
1715280534
Hero Member
*
Offline Offline

Posts: 1715280534

View Profile Personal Message (Offline)

Ignore
1715280534
Reply with quote  #2

1715280534
Report to moderator
AdolfinWolf
Legendary
*
Offline Offline

Activity: 1946
Merit: 1427


View Profile
November 23, 2021, 04:29:09 PM
 #2

you need to define rows (const rows = client.query) before iterating

or maybe you can do what you're doing right now but use for r in result ?

But, errrm, what does this have to do with bitcoin? Don't think this is a general technical discussion board. Use Stackoverflow in the future.



The developer I work with have been stuck with this issue for few days now and I need help from someone experienced in js to answer this.



I seriously hope you're not paying him per hour

PawGo
Legendary
*
Offline Offline

Activity: 952
Merit: 1367


View Profile
November 23, 2021, 07:56:44 PM
 #3

try
Code:
client.query(`SELECT * FROM colours_strength_table WHERE id == ${id}`,
        function(err, rows) {
            for (let r in rows) {
               
            }
        })
Coinoplex (OP)
Copper Member
Sr. Member
****
Offline Offline

Activity: 250
Merit: 412


Building A Crypto Empire


View Profile
November 23, 2021, 11:27:01 PM
 #4

you need to define rows (const rows = client.query) before iterating

or maybe you can do what you're doing right now but use for r in result ?

But, errrm, what does this have to do with bitcoin? Don't think this is a general technical discussion board. Use Stackoverflow in the future.



The developer I work with have been stuck with this issue for few days now and I need help from someone experienced in js to answer this.



I seriously hope you're not paying him per hour

Thanks for the fix, I have just changed res to rows instead.

Does the forum have general tech discussion board? This question is related to a crypto method I am building in the exchange but of course had to change the names and params in the code before posting for security reasons, having that all said I still understand where you are coming from.

Regards to the developer I guess he might be busy with other projects etc, adding to that I want to say that he is not getting paid per hour but has instead % in the project.

I am looking for another dev as well to join, will you be interested in your spare time if possible? Even if it means as part time.  Shocked

try
Code:
client.query(`SELECT * FROM colours_strength_table WHERE id == ${id}`,
        function(err, rows) {
            for (let r in rows) {
               
            }
        })

Thanks for the fix it works. Appreciated.

/////////////

The second issue I am facing is json result being returned as “success”

So have changed this line to:
if (strength === null && colour === null) { return res.json("no strength and colour params found in DB": false)}

I assume the code is not fetching correctly from DB the colour and strength as per this code:
if (r["id"] === id) {
                    strength = r["strength"]
                    colour = r["colour"]


How can this be fixed? I did check DB in terminal and have found the correct table with the correct params saved inside it etc, but why is node not able to get those params.
AdolfinWolf
Legendary
*
Offline Offline

Activity: 1946
Merit: 1427


View Profile
November 25, 2021, 06:17:28 PM
 #5


How can this be fixed? I did check DB in terminal and have found the correct table with the correct params saved inside it etc, but why is node not able to get those params.
Parse the json? (.json())

See https://developer.mozilla.org/en-US/docs/Web/API/Response/json

Coinoplex (OP)
Copper Member
Sr. Member
****
Offline Offline

Activity: 250
Merit: 412


Building A Crypto Empire


View Profile
November 28, 2021, 11:22:03 PM
 #6


How can this be fixed? I did check DB in terminal and have found the correct table with the correct params saved inside it etc, but why is node not able to get those params.
Parse the json? (.json())

See https://developer.mozilla.org/en-US/docs/Web/API/Response/json

Thanks for that.

Update: All this method is completed now.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!