2016-05-29 27 views
0

こんにちは、私はこのエラーをserver.jsに入れています。だから私はスタックフローで多くの答えを読むことを試みたが、私は答えを得られなかった。だから親切に、このコードを編集したり、何が問題なのか教えてください。 Error is this.get()はコールバック関数が必要ですが、ルートで[オブジェクト未定義]を取得しました。(匿名関数)get []

Index.js

'use strict'; 
 

 
var controller = require('./look.controller'); 
 
var express = require('express'); 
 
var router = express.Router(); 
 
var auth = require('../../auth/auth.service'); 
 

 
router.post('/scrapeUpload', auth.isAuthenticated(), controller.scrapeUpload); 
 
router.post('/upload', auth.isAuthenticated(), controller.upload); 
 

 
router.put('/:id', auth.isAuthenticated(), controller.update); 
 

 
router.get('/getAllLooks', controller.allLooks); 
 
router.get('/getUserLooks', controller.userLooks); 
 
router.get('/:lookId', controller.singleLook); 
 
router.get('/popLooks/:id', controller.popLooks); 
 

 
router.delete('/:id', controller.delete); 
 

 
module.exports = router;

Route.js

/*! 
 
* express 
 
* Copyright(c) 2009-2013 TJ Holowaychuk 
 
* Copyright(c) 2013 Roman Shtylman 
 
* Copyright(c) 2014-2015 Douglas Christopher Wilson 
 
* MIT Licensed 
 
*/ 
 

 
'use strict'; 
 

 
/** 
 
* Module dependencies. 
 
* @private 
 
*/ 
 

 
var debug = require('debug')('express:router:route'); 
 
var flatten = require('array-flatten'); 
 
var Layer = require('./layer'); 
 
var methods = require('methods'); 
 

 
/** 
 
* Module variables. 
 
* @private 
 
*/ 
 

 
var slice = Array.prototype.slice; 
 
var toString = Object.prototype.toString; 
 

 
/** 
 
* Module exports. 
 
* @public 
 
*/ 
 

 
module.exports = Route; 
 

 
/** 
 
* Initialize `Route` with the given `path`, 
 
* 
 
* @param {String} path 
 
* @public 
 
*/ 
 

 
function Route(path) { 
 
    this.path = path; 
 
    this.stack = []; 
 

 
    debug('new %s', path); 
 

 
    // route handlers for various http methods 
 
    this.methods = {}; 
 
} 
 

 
/** 
 
* Determine if the route handles a given method. 
 
* @private 
 
*/ 
 

 
Route.prototype._handles_method = function _handles_method(method) { 
 
    if (this.methods._all) { 
 
    return true; 
 
    } 
 

 
    var name = method.toLowerCase(); 
 

 
    if (name === 'head' && !this.methods['head']) { 
 
    name = 'get'; 
 
    } 
 

 
    return Boolean(this.methods[name]); 
 
}; 
 

 
/** 
 
* @return {Array} supported HTTP methods 
 
* @private 
 
*/ 
 

 
Route.prototype._options = function _options() { 
 
    var methods = Object.keys(this.methods); 
 

 
    // append automatic head 
 
    if (this.methods.get && !this.methods.head) { 
 
    methods.push('head'); 
 
    } 
 

 
    for (var i = 0; i < methods.length; i++) { 
 
    // make upper case 
 
    methods[i] = methods[i].toUpperCase(); 
 
    } 
 

 
    return methods; 
 
}; 
 

 
/** 
 
* dispatch req, res into this route 
 
* @private 
 
*/ 
 

 
Route.prototype.dispatch = function dispatch(req, res, done) { 
 
    var idx = 0; 
 
    var stack = this.stack; 
 
    if (stack.length === 0) { 
 
    return done(); 
 
    } 
 

 
    var method = req.method.toLowerCase(); 
 
    if (method === 'head' && !this.methods['head']) { 
 
    method = 'get'; 
 
    } 
 

 
    req.route = this; 
 

 
    next(); 
 

 
    function next(err) { 
 
    if (err && err === 'route') { 
 
     return done(); 
 
    } 
 

 
    var layer = stack[idx++]; 
 
    if (!layer) { 
 
     return done(err); 
 
    } 
 

 
    if (layer.method && layer.method !== method) { 
 
     return next(err); 
 
    } 
 

 
    if (err) { 
 
     layer.handle_error(err, req, res, next); 
 
    } else { 
 
     layer.handle_request(req, res, next); 
 
    } 
 
    } 
 
}; 
 

 
/** 
 
* Add a handler for all HTTP verbs to this route. 
 
* 
 
* Behaves just like middleware and can respond or call `next` 
 
* to continue processing. 
 
* 
 
* You can use multiple `.all` call to add multiple handlers. 
 
* 
 
* function check_something(req, res, next){ 
 
*  next(); 
 
* }; 
 
* 
 
* function validate_user(req, res, next){ 
 
*  next(); 
 
* }; 
 
* 
 
* route 
 
* .all(validate_user) 
 
* .all(check_something) 
 
* .get(function(req, res, next){ 
 
*  res.send('hello world'); 
 
* }); 
 
* 
 
* @param {function} handler 
 
* @return {Route} for chaining 
 
* @api public 
 
*/ 
 

 
Route.prototype.all = function all() { 
 
    var handles = flatten(slice.call(arguments)); 
 

 
    for (var i = 0; i < handles.length; i++) { 
 
    var handle = handles[i]; 
 

 
    if (typeof handle !== 'function') { 
 
     var type = toString.call(handle); 
 
     var msg = 'Route.all() requires callback functions but got a ' + type; 
 
     throw new TypeError(msg); 
 
    } 
 

 
    var layer = Layer('/', {}, handle); 
 
    layer.method = undefined; 
 

 
    this.methods._all = true; 
 
    this.stack.push(layer); 
 
    } 
 

 
    return this; 
 
}; 
 

 
methods.forEach(function(method){ 
 
    Route.prototype[method] = function(){ 
 
    var handles = flatten(slice.call(arguments)); 
 

 
    for (var i = 0; i < handles.length; i++) { 
 
     var handle = handles[i]; 
 

 
     if (typeof handle !== 'function') { 
 
     var type = toString.call(handle); 
 
     var msg = 'Route.' + method + '() requires callback functions but got a ' + type; 
 
     throw new Error(msg); 
 
     } 
 

 
     debug('%s %s', method, this.path); 
 

 
     var layer = Layer('/', {}, handle); 
 
     layer.method = method; 
 

 
     this.methods[method] = true; 
 
     this.stack.push(layer); 
 
    } 
 

 
    return this; 
 
    }; 
 
});

look.controller

'use strict'; 
 

 
var _ = require('lodash'); 
 
var Look = require('./look.model'); 
 
var path = require('path'); 
 
var express = require('express'); 
 
var utils = require('../../utils/utils.js'); 
 

 
exports.allLooks = function(req, res) { 
 
    Look.find({}) 
 
    .sort({ 
 
     createTime: -1 
 
    }) 
 
    .exec(function(err, looks) { 
 
     if (err) { 
 
     return handleError(res, err); 
 
     } 
 
     if (!looks) { 
 
     return res.send(404); 
 
     } 
 
     console.log(looks); 
 
     return res.status(200) 
 
        .json(looks); 
 
    }); 
 
}; 
 

 
exports.userLooks = function(req, res) { 
 
    var userEmail = req.query.email; 
 
    Look.find({ 
 
    email: { 
 
     $in: userEmail 
 
    } 
 
    }) 
 
    .sort({ 
 
    createTime: -1 
 
    }) 
 
    .exec(function(err, looks) { 
 
    if(err) { 
 
     return handleError(res, err); 
 
    } 
 
    console.log(looks); 
 
    return res.status(200) 
 
        .json(looks); 
 
    }); 
 
}; 
 

 
exports.scrapeUpload = function(req, res) { 
 
    var random = utils.randomizer(32, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); 
 

 
    utils.downloadURI(req.body.image, '../client/assets/images/uploads/' + random + '.png', function(filename) { 
 
    console.log('done'); 
 

 
    var newLook = new Look(); 
 
    newLook.title = req.body.title; 
 
    newLook.image = filename.slice(9); 
 
    newLook.email = req.body.email; 
 
    newLook.linkURL = req.body.linkURL; 
 
    newLook.description = req.body.description; 
 
    newLook.userName = req.body.name; 
 
    newLook._creator = req.body._creator; 
 
    newLook.createTime = Date.now(); 
 
    newLook.upVotes = 0; 
 
    newLook.save(function(err, item) { 
 
     if (err) { 
 
     console.log('error occured in saving post'); 
 
     } else { 
 
     console.log('Success post saved'); 
 
     console.log(item); 
 
     res.status(200) 
 
      .json(item); 
 
     } 
 
    }); 
 
    }); 
 
} 
 

 
exports.upload = function(req, res) { 
 
    var newLook = new Look(); 
 
    var fileimage = req.middlewareStorage.fileimage; 
 

 
    console.log(req.body); 
 
    newLook.image = '/assets/images/uploads/' + fileimage; 
 
    newLook.email = req.body.email; 
 
    newLook.linkURL = req.body.linkURL; 
 
    newLook.title = req.body.title; 
 
    newLook.description = req.body.description; 
 
    newLook.userName = req.body.name; 
 
    newLook._creator = req.body._creator; 
 
    newLook.createTime = Date.now(); 
 
    newLook.upVotes = 0; 
 

 
    newLook.save(function(err, look) { 
 
    if(err) { 
 
     console.log('error saving look'); 
 
     return res.send(500); 
 
    } else { 
 
     console.log(look); 
 
     res.status(200) 
 
      .send(look); 
 
    } 
 
    }); 
 
}; 
 

 
exports.singleLook = function(req, res) { 
 
    Look.findById(req.params.lookId, function(err, look) { 
 
    if(err) { 
 
     return handleError(res, err); 
 
    } 
 
    if(!look) { 
 
     return res.send(404); 
 
    } 
 
    return res.json(look); 
 
    }); 
 
}; 
 

 
exports.update = function(req, res) { 
 
    if(req.body._id) { 
 
    delete req.body._id; 
 
    } 
 
    Look.findById(req.params.id, function(err, look) { 
 
    if(err) { 
 
     return handleError(res, err); 
 
     } 
 
     if(!look) { 
 
     return res.send(404); 
 
     } 
 
     var updated = _.merge(look, req.body); 
 
     updated.save(function(err) { 
 
     if(err) { 
 
      return handleError(res, err); 
 
     } 
 
     console.log(look); 
 
     return res.json(look); 
 
     }); 
 
    }); 
 
}; 
 

 
exports.delete = function(req, res) { 
 
    Look.findById(req.params.id, function(err, look) { 
 
    if(err) { 
 
     return handleError(res, err); 
 
    } 
 
    if(!look) { 
 
     return res.send(404); 
 
    } 
 
    look.remove(function(err) { 
 
     if(err) { 
 
     return handleError(res, err); 
 
     } 
 
     return res.send(200); 
 
    }); 
 
    }); 
 
}; 
 

 
function handleError(res, err) { 
 
    return res.send(500, err); 
 
}

+2

仮説: 'controller.popLooks_or_whatever'プロパティの1つが' undefined'と評価されます。 – user2864740

+0

私は初心者です。あなたはそれを修正できますか? –

+1

私は、 'router.get( '/ someroute'、undefined);'と同等のものによってエラーが引き起こされていると思われます。これは、コントローラメソッドの1つ(例えば、 'popLooks')が存在しないか、使用されているものとは異なる名前を持つ場合に起こります。コントローラー(コールバック)関数ではなく、 'controller.popLooks' - >' undefined'となります。質問にはおそらく 'look.controller.js'も含まれているはずです。 – user2864740

答えて

0

あなたはrouter.get('/popLooks/:id', controller.popLooks);

controller.popLooksを行い、定義されていません。

exports.popLookslook.controllerに定義するとうまくいくはずです。

0

あなたはlook.controllerにおける関数定義が欠落しています。そのファイルを貼り付けてください。スタックトレースはindex.jsの16行目の問題を示しています。それは[Object undefined]として評価されるように

+0

がアップロードされました。チェックしてください –

+0

user2864740 as missing popLooks –

+0

私はどこにエラーがありますが、それを修正する方法がありますか? –

関連する問題