Nodejs中的回调函数错误

时间:2014-10-29 06:58:06

标签: javascript node.js

var bcrypt = require('bcrypt-nodejs');
var request = require('request');
var rest_api = require('../../config/rest_api');
var target = "sivatest";

function chatMessages(user_name, target) {
    this.local = {
        user_name: user_name,
        target: target
    };
}

chatMessages.sendChatMessage = function (user_name, target, content) {
    request.post({
        url: rest_api.message + user_name + '/' + target,
        body: {
            content: content
        },
        json: true
    }, function (err, res) {
        console.log("coming back after save chatMessages");
        if (err || res.statusCode !== 200) {
            console.log("return unsuccessfully!");
            console.log(res.statusCode);
        } else {
            console.log("return successfully");
        }
        return;
    });

};

chatMessages.getChatMessage = function (user_name, target, callback) {

    request(rest_api.messages + '/' + user_name + '/' + target, {
        json: true
    }, function (err, res, body) {
        console.log("test get");
        console.log(res);
        console.log(res.statusCode);
        if (err) {
            callback(err, res);
            return;
        }
        if (res.statusCode === 200) {
            //window.alert("success");
            callback(null, body);
            return;
        }
        if (res.statusCode !== 200) {
            //window.alert("null");
            callback(null, null);
            return;
        }
    });
};

module.exports = chatMessages;

在上面的代码中,我能够从后端服务器获取消息,并将状态代码显示为200并正确检索消息。但是,我在回调函数中收到此错误,其中状态代码为200:

TypeError: undefined is not a function
    at Request._callback (/Users/ranjani/fse-F14-SA3-SSNoC-Node-UI/app/models/chatRest.js:38:17)
    at Request.self.callback (/Users/ranjani/fse-F14-SA3-SSNoC-Node-UI/node_modules/request/request.js:121:22)
    at Request.emit (events.js:98:17)
    at Request. (/Users/ranjani/fse-F14-SA3-SSNoC-Node-UI/node_modules/request/request.js:985:14)
    at Request.emit (events.js:117:20)
    at IncomingMessage. (/Users/ranjani/fse-F14-SA3-SSNoC-Node-UI/node_modules/request/request.js:936:12)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickCallback (node.js:419:13

有谁可以建议我哪里错了?

0 个答案:

没有答案