lundi 22 août 2016

Nodejs Request values

So I have a nodejs server and I am trying to make comparisons to the req values. Firstly, here is my code:

app.post('/', function(req, res) {
    firstName = req.body.firstName;
    lastName = req.body.lastName;
    message = req.body.message;
    token = req.body.token;
    user = {name: firstName + " " + lastName, token: token};
    selectedUser = req.body.selectedUser;
    users.push(user);
    console.log(user.name);
    
    if (req.body.isAndroid === true) {
        sendToAndroid(); //add message parameter
    } else {
        sendToios(); //add message parameter
    }
});

app.listen(8080, function() {
    console.log('running on port 8080');
});
//GCM
function sendToAndroid() {
    var message = new gcm.Message();
    var tokenLocation;
    //API Server Key
    var sender = new gcm.Sender('AIzaSyD-B3EG1xpMh6YhwBKfLMyw0GIQKWfGgZM');
        
    //console.log(message);
    // Value the payload data to send...
    message.addData({
        title: 'Hello',
        body: 'Message From: ' + user.name + ': ' + message,
        msgcnt: 1,
        timeToLive: 3000 
    });
    
    // At least one reg id required
    if (registrationToken.indexOf(token) == -1) {
        registrationToken.push(token);
        tokenLocation = registrationToken.indexOf(token);
    } else {
        tokenLocation = registrationToken.indexOf(token);
    }
    
    if (users.indexOf(user.name) == -1) {
        console.log("user destination not found");
    } else {
        var userTokenArray = [];
        userTokenArray.push(user.token);
        sender.send(message, { registrationTokens: userTokenArray } , function (err, response) {
            if(err) console.error(err);
            else    console.log(response);    
        });
        userTokenArray.pop();
    }
    
}

And here is my problem when outputting to see what the value is:

running on port 8080
undefined undefined
user destination not found

What I am trying to do is put the registered users into an array of users that each element has a full name and token. Then in the Android function, it will check to see what value value is selected and then push a notification to the selectedUser via their token. I am so confused on how to compare the "strings" or whatever they are. I am using nodejs express with body-parser. Any help is appreciated, I need to figure this out. Thanks!

Aucun commentaire:

Enregistrer un commentaire