2015-06-22 60 views
0

我想創建回調貓鼬object.save函數,但我geting response.sent未定義。我在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) 
+0

最後一行'module.exports = DataCallbac'。這個錯字是否是無意的,還是它在你的實際代碼中是一個錯字? –

+0

另外,'var dataCallbacks = require('../ Utils/DataCallbacks');',模塊名稱是'DataCallback'。這是另一個錯字嗎? –

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

    } 
}