大家好我在server.js中遇到这个错误没有这样的错误,因为我是初学者,所以我试图在stackover流程上阅读很多答案,但我没有得到答案。那么有人可以编辑这段代码或告诉我有什么问题吗?Error is this
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;
};
});
&#13;
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, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
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);
}
&#13;
答案 0 :(得分:0)
您在look.controller
中缺少功能定义。请粘贴该文件。另请注意,堆栈跟踪显示问题是index.js的第16行。
答案 1 :(得分:0)
执行router.get('/popLooks/:id', controller.popLooks);
controller.popLooks
未定义,因此评估为[Object undefined]
。
在exports.popLooks
中定义look.controller
,它应该有效。