2016-12-17 3 views
0

mein server.js Code:json Daten condtion für die Suche nach

var express = require('express'); 
var app = express(); 


// set the view engine to ejs 
app.set('view engine', 'ejs'); 
app.use(express.static(__dirname + '/dist')); 
if (par.query.login) { 


var data={ 
    "user": { 
     "displayName": "fares alkhawaja", 
     "username": "elkhawajah" 
    }, 
    "profile": { 
      "photo":"null", 
      "fullName": "fares sameer alkhawaja" 
    }, 
    "balance": { 
     "overall": 200, 
     "outstanding": 149 
    }, 
    "currentTasks": [{ 
     "isProject": false, 
     "id": "1234", 
     "title": "Build NodeJS Application", 
     "description": "This is a description for building nodejs application", 
     "prize": "$500", 
     "project": "topcoder", 
     "holder": "elkhawajah" 
    }, { 
     "id": "134", 
     "isProject": false, 
     "title": "Build NodeJS Application", 
     "description": "This is a description for building nodejs application", 
     "prize": "$500", 
     "project": "topcoder", 
     "holder": "elkhawajah" 
    }, { 
     "id": "12", 
     "isProject": true, 
     "title": "Build NodeJS Application", 
     "description": "This is a description for building nodejs application", 
     "prize": "$500", 
     "project": "topcoder", 
     "holder": "elkhawajah" 
    }], 
    "pastTasks": [{ 
     "isProject": false, 
     "id": "1", 
     "title": "Build NodeJS Application", 
     "description": "This is a description for building nodejs application", 
     "prize": "$500", 
     "project": "topcoder", 
     "holder": "fares" 
    }, { 
     "id": "2", 
     "isProject": false, 
     "title": "Build NodeJS Application", 
     "description": "This is a description for building nodejs application", 
     "prize": "$500", 
     "project": "topcoder", 
     "holder": "fares" 
    }, { 
     "id": "3", 
     "isProject": true, 
     "title": "Build NodeJS Application", 
     "description": "This is a description for building nodejs application", 
     "prize": "$500", 
     "project": "topcoder", 
     "holder": "fares" 
    }], 
    "timeline": [{ 
     "title": "Fares uploaded a new submission", 
     "project": "tasqat", 
     "task": "Build new endpoint", 
     "date": new Date(), 
     "handle": "fares" 
    }, { 
     "title": "Fares uploaded a new submission", 
     "project": "tasqat", 
     "task": "Build new endpoint", 
     "date": new Date(), 
     "handle": "fares" 
    }] 
} 
} 
app.get("/dashboard", function(req, res) { 

//send Jsondata to /view/dashboard.ejs 
res.render('dashboard', data); 

}); 

app.listen(process.env.PORT); 
console.log(process.env.PORT + ' is the magic port'); 

mein Problem ist, ich brauche req.query.login zu verwenden ... wenn req.query.login {Daten zurückgeben} else {nichts zurückgeben }

zum Beispiel .. wenn ich http://localhost:3000/dashboard Daten verwenden sollte nichts zurückgeben, aber wenn Benutzeranmeldung in http://localhost:3000/dashboard?login=true sollte die Daten zurückgeben ... irgendwelche Hilfe?

+0

Unklare Frage sein. – nicovank

+0

@nicovank meine Frage ist: Wie json Daten unter Bedingung zu verwenden ... if (req.query.login) {var data = einige Daten} sonst, wenn nicht req.query.login var data = null .... wie wenn http: // localhost: 3000/preise drucken nicht die daten, aber wenn ich http: // localhost: 3000/tarife benutzen? login = true drucke die daten –

+0

Zeigen sie ihren 'controller' code hier –

Antwort

0

Wie pro Ihre Anforderung eine Probe-Controller mit Bedingung für query params

var userData = {...}; //your data for user 

Der Controller wird so etwas wie dieses

app.get("/dashboard", function(req, res) { 

    var isLoggedIn = req.query.login; //assuming route as /dashboard?login=true 
    var data = isLoggedIn? userData : {}; //empty object or null, whatever you want 

    res.render('dashboard', data); 

}); 
+0

thnx @Jyothi Babu Araja für Ihre Antwort .. ich var Daten vor app.get Dashboard und fügen Sie Ihren Code in app.get .. aber geben undefined Daten zurück –

+0

@faresalkhawjaa Sie bekommen 'undefined' auch' login = true'? –

+0

/dashboard? Login = true seine Arbeit ... aber die Seite/das Dashboard ist zurück undefined –