0

I am trying to get my code ready to send json back to my chatbot, can someone please tell me why i am getting an error on this:

    const data = '{ "Company Name": companyname, "service type": service }'
        try {
          const user = JSON.parse(data)
        } catch(err) {
          console.error(err)
}

this is my full lower code all working apart from this

//=======================================================================================
//  Get the Client data from database
//=======================================================================================
app.get("/Getcompany", function(request, response) {
  const cname = request.query.cname, query = `select * from clientdata_nsw where companyname  = ${connection.escape(
    cname
  )}`;
  connection.query(query, function(err, rows) {
    if (err) {
      console.log(err);
      return;
    }
    const data = '{ "Company Name": companyname, "service type": service }'
        try {
          const user = JSON.parse(data)
        } catch(err) {
          console.error(err)
}
    rows.forEach(function(result) {
      console.log(
        result.companyname,
        result.service,
        result.phone,
        result.open_times,
        result.rating_facebook,
        result.rating_goggle
      );
    });
    response.json({})
  });

})



  // listen for requests :)
var listener = app.listen(process.env.PORT, function () {
  console.log('Your app is listening on port ' + listener.address().port);
});
4
  • What is the error you are currently getting? Commented Mar 22, 2019 at 10:48
  • C:\Users\Adam.Wolarczuk\Desktop\Projects\nodetest>node db.js Your app is listening on port 60402 (node:22868) [DEP0096] DeprecationWarning: timers.unenroll() is deprecated. Please use clearTimeout instead. C:\Users\Adam.Wolarczuk\Desktop\Projects\nodetest\node_modules\mysql\lib\protocol\Parser.js:78 throw err; // Rethrow non-MySQL errors ^ SyntaxError: Unexpected token c in JSON at position 18 at JSON.parse (<anonymous>) at Query._callback C:\Users\Adam.Wolarczuk\Desktop\Projects\nodetest> Commented Mar 24, 2019 at 9:44
  • @AdamWolarczuk Can you console the value of cname? are you getting an actual value? Commented Mar 24, 2019 at 13:06
  • i sure am i am getting the company name is am passing Commented Mar 25, 2019 at 0:52

1 Answer 1

1

Error is present in the first line itself

const data = '{ "Company Name": companyname, "service type": service }'
JSON.parse(data)

You have to escape companyname and service because those cannot be parsed by JSON parser else use a JS object rather than parsing a string and forming a JS object. I am not understanding why you have those lines in the code because they are not used for anything at all.

const data = '{ "Company Name": "companyname", "service type": "service" }'
JSON.parse(data)

The entire code rewritten using ES6 standards.

const PORT = process.env.PORT;

app.get("/Getcompany", async (request, response) => {
  const cname = request.query.cname,
    query = `select * from clientdata_nsw where companyname  = ${connection.escape(
      cname
    )}`;
  const data = '{ "Company Name": "companyname", "service type": "service" }';
  try {
    const user = JSON.parse(data);
  } catch (err) {
    console.error(err);
  }
  try {
    const results = await connection.query(query);
    results.forEach(row => {
      console.log(
        row.companyname,
        row.service,
        row.phone,
        row.open_times,
        row.rating_facebook,
        row.rating_goggle
      );
    });
  } catch (error) {
    console.error(error);
  }
  response.json({});
});

app.listen(PORT, () => {
  console.log(`Your app is listening on port ${PORT}`);
});
Sign up to request clarification or add additional context in comments.

4 Comments

I am using it in this part ); }); response.json({}) }); }) knowing me i am doing it completely wrong :)
i added it under the row.foreach and it came up with the same issue
just a question why am i adding the parse twice if you look i am adding it under the try loop
yep ok i think i found the issue const data = '{ "Company Name": "companyname", "service type": "service" }'; is not storing showing my responces from the database , any ideas

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.