Nodejs请求值

时间:2016-08-22 23:41:09

标签: json node.js express webserver

所以我有一个nodejs服务器,我正在尝试与req值进行比较。首先,这是我的代码:

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();
    }

}

输出以查看值是什么时,这是我的问题:

running on port 8080
undefined undefined
user destination not found

我要做的是将注册用户放入一个用户数组中,每个元素都有一个全名和令牌。然后在Android功能中,它将检查选择的值是什么,然后通过其令牌将通知推送到selectedUser。我对如何比较“字符串”或它们是什么感到困惑。我正在使用nodejs express与body-parser。

0 个答案:

没有答案