如何为猫鼬创建回调

时间:2015-06-22 19:48:03

标签: javascript node.js mongodb express mongoose

我正在尝试为mongoose object.save函数创建回调,但我正在回应。未定义。我是nodejs的新手。如果有人能指导我,我会非常感激。

DataCallback.js

function DataCallback(req, res, next, model) {
    this.request = req;
    this.response = res;
    this.next = next;
    this.model = model;
    console.log(model+"-----------------------");
    var res = {status: 200};

    this.response.send(res);
}

DataCallback.prototype.insert = function (err, data) {

    if (err || data == undefined || data == null) {
        console.log(err);
        var res = {status: 1000, error: "unable to create " + this.model};

        this.response.send(res);
    }
    else {
        var res = {status: 200, record: data};

        this.response.send(res);
    }

    this.next();
}

module.exports = DataCallbac

Controller.js

var express=require('express');
var Router=express.Router();
var Group=require('../Model/Group');
var ObjectId=require("mongoose").ObjectId;
var dataCallbacks=require('../Utils/DataCallbacks');

Router.post("/",function(req,res,next){

    var group=new Group(req.body);



    group.save(new dataCallbacks(req,res,next,"Group").insert);

});

错误

  if (this.ended && !this.hasRejectListeners()) throw reason;
                                                      ^
TypeError: Cannot call method 'send' of undefined
    at EventEmitter.DataCallback.insert (/media/qasim/827E73097E72F4EF/node-test/primzel-api/Utils/DataCallbacks.js:23:23)
    at EventEmitter.<anonymous> (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45)
    at EventEmitter.emit (events.js:98:17)
    at Promise.safeEmit (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21)
    at Promise.fulfill (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:94:24)
    at Promise.resolve (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/lib/promise.js:113:23)
    at model.<anonymous> (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/lib/document.js:1569:39)
    at next_ (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:89:34)
    at EventEmitter.fnWrapper (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:171:15)
    at EventEmitter.<anonymous> (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45)
    at EventEmitter.emit (events.js:98:17)
    at Promise.safeEmit (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21)
    at Promise.fulfill (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:94:24)
    at p1.then.then.self.isNew (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/lib/model.js:254:27)
    at newTickHandler (/media/qasim/827E73097E72F4EF/node-test/primzel-api/node_modules/mongoose/node_modules/mpromise/lib/promise.js:229:18)
    at process._tickCallback (node.js:448:13)

1 个答案:

答案 0 :(得分:0)

它对我有用:)

更改控制器操作

group.save(new dataCallbacks(req,res,next,"Group").insert());

更改DataCallback.js

DataCallback.prototype.insert = function () {

    var req=this.request;
    var resp=this.response;
    var next=this.next;
    model=this.model;

    return function (err, data) {

        if (err || data == undefined || data == null) {
            console.log(err);
            var res = {status: 1000, error: "unable to create " + model};

            resp.send(res);
        }
        else {
            var res = {status: 200, record: data};

            resp.send(res);
        }

    }
}