Convert sql object to valid Json string in node.js - Azure

前端 未结 4 864
轻奢々
轻奢々 2021-01-07 03:57

We are creating a web service in Azure service using node.js to retrieve data from SQL db. We are using ClearDB to do the same.

While retriving the data its not comm

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 04:38

    The response from the SQL service is JSON - as you have shown. You need to use JSON.parse() to parse the JSON into an object. Something like:

    app.get('/android', function(request, response) {
        pool.getConnection(function(err, connection) {
            if(err) { handleErrorResponse(err, response); return; }
                var sql = "select projectname from taggedemployee where empname='test@hotmail.com' and tagflag='accepted'"
            connection.query(sql, {}, function(err, results) {
                  connection.release(); // always put connection back in pool after last query
              if(err) { handleErrorResponse(err, response); return;  }
                    var proj = JSON.parse(response);
                    console.log(proj);
              response.setHeader('Content-Type', 'application/json');
              response.status(200).send(results);
    
            });
        });
    });
    

    JSON.stringify is used to convert an object into a JSON string. JSON.parse is used to convert a JSON string into an object.

提交回复
热议问题