Question-er XDD
Question-er XDD

Reputation: 767

Express/Parse - cannot get respond back

I have written some script to get the data from my parse server(http://parseplatform.org/). But i cannot get the result, may i ask for some advice on it. i understand parse.query is a "promise function", but it should work with simple get/post request?

app.get('/api/getInstant/', function (req, res) {

    let classname = req.query["cid"]; //cid
    let colname = req.query["col"]; //col   

    console.log('OK-1: '+classname+'   '+colname+"\n\r");   

    Parse.initialize(ServerInfo['appId'],ServerInfo['masterKey'],ServerInfo['masterKey'])
    Parse.serverURL = ServerInfo['serverURL']

    let ReadClass = Parse.Object.extend(classname);
    let query = new Parse.Query(ReadClass);

    query.exists(colname);
    query.descending(DecendingSortBy);
    query.limit(1);

    console.log('OK-2: Start Query\n\r');   

    query.find().then((results) => {
      // results is an array of parse.object.
        _.each(results, function(result) {
        let search_result = null
        search_result= result.get(colname);
        console.log('OK-3: '+search_result+"\n\r");     
        res.sendStatus(search_result);          
      })
    }).catch((error) =>  {});

});

This is the result i get in my console: Example app listening at http://:::8080

OK-1: YoHandsome   OutTemp

OK-2: Start Query

OK-3: 27.2

Updated @ 180810-1631 Error Message

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 27.2
    at ServerResponse.writeHead (_http_server.js:201:11)
    at ServerResponse._implicitHeader (_http_server.js:192:8)
    at write_ (_http_outgoing.js:637:9)
    at ServerResponse.end (_http_outgoing.js:760:5)
    at ServerResponse.send (/home/ubuntu/MyComputer-Server/DashboardWeb/node_modules/express/lib/response.js:221:10)
    at ServerResponse.sendStatus (/home/ubuntu/MyComputer-Server/DashboardWeb/node_modules/express/lib/response.js:359:15)
    at /home/ubuntu/MyComputer-Server/DashboardWeb/WebServerLauncher.js:50:7
    at Function._.each._.forEach (/home/ubuntu/MyComputer-Server/DashboardWeb/node_modules/underscore/underscore.js:186:9)
    at query.find.then (/home/ubuntu/MyComputer-Server/DashboardWeb/WebServerLauncher.js:46:7)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:182:7)

Upvotes: 0

Views: 73

Answers (1)

Suresh Prajapati
Suresh Prajapati

Reputation: 4477

Your code is correct, only probem is you trying to send an invalid HTTP Status Code

Since it's a success case you can send 200 as status code and the respective data may go in req body of response.

app.get('/api/getInstant/', function (req, res) {
let classname = req.query["cid"]; //cid
let colname = req.query["col"]; //col   

console.log('OK-1: '+classname+'   '+colname+"\n\r");   

Parse.initialize(ServerInfo['appId'],ServerInfo['masterKey'],ServerInfo['masterKey'])
Parse.serverURL = ServerInfo['serverURL']

let ReadClass = Parse.Object.extend(classname);
let query = new Parse.Query(ReadClass);

query.exists(colname);
query.descending(DecendingSortBy);
query.limit(1);

console.log('OK-2: Start Query\n\r');   

query.find().then((results) => {
  // results is an array of parse.object.
    _.each(results, function(result) {
    let search_result = null
    search_result= result.get(colname);
    console.log('OK-3: '+search_result+"\n\r");     
    res.status(200).json({data: search_result});      
  })
}).catch((error) =>  {res.status(500).json({msg: 'Sorry, something went wrong!'})});

});

Upvotes: 1

Related Questions