当变量是函数参数时应用抛出变量引用错误
App throwing variable reference error when variable is a function argument
我在 MEAN 堆栈中构建后端时遇到了一个问题。该应用程序的要点很简单:上传文件并将其保存到服务器。但是,我偶然发现了一个问题。当我点击 "Submit" 按钮时,代码中断并在控制台中抛出以下错误:
<h1>photo is not defined</h1>
<h2></h2>
<pre>ReferenceError: photo is not defined
at \routes\index.js:32:3
at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5)
at next (\node_modules\express\lib\router\route.js:131:13)
at \node_modules\express-jwt\lib\index.js:112:9
at \node_modules\jsonwebtoken\index.js:98:18
at process._tickCallback (node.js:355:11)</pre>
以下是我的angularApp.js
文件:
app.factory('photos', ['$http', 'auth', function($http, auth){
var o = { photos: [] };
o.create = function(photo) {
return $http.post('/photos', photo, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
o.photos.push(data);
}).error(function(a) {
console.log(a);
});
};
return o;
}]);
app.controller('MainCtrl', [
'$scope',
'photos',
'auth',
function($scope, photos, auth){
$scope.photos = photos.photos;
$scope.addPhoto = function(){
photos.create();
};
}]);
index.ejs
代码:
<form ng-submit="addPhoto()">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile">
<button type="submit" class="btn btn-primary">Post</button>
</form>
这里是routes\index.js
router.post('/photos', auth, function(req, res, next) {
var dirname = require('path').dirname(__dirname);
var filename = req.files.file.name;
var path = req.files.file.path;
var type = req.files.file.mimetype;
var read_stream = fs.createReadStream(dirname + '/' + path);
var conn = req.conn;
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var gfs = Grid(conn.db);
var writestream = gfs.createWriteStream({
filename: filename
});
read_stream.pipe(writestream);
photo.save(function(err, photo){
if(err){ return next(err); }
res.json(photo);
});
});
据我所知,代码破坏了 运行 o.create
函数,但我不确定为什么它无法找到 photo
,而这是函数参数之一.
使用您为 routes/index.js
发布的代码,行
photo.save(...
正在使用以前未在当前或任何父范围中定义的变量 photo
。
我在 MEAN 堆栈中构建后端时遇到了一个问题。该应用程序的要点很简单:上传文件并将其保存到服务器。但是,我偶然发现了一个问题。当我点击 "Submit" 按钮时,代码中断并在控制台中抛出以下错误:
<h1>photo is not defined</h1>
<h2></h2>
<pre>ReferenceError: photo is not defined
at \routes\index.js:32:3
at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5)
at next (\node_modules\express\lib\router\route.js:131:13)
at \node_modules\express-jwt\lib\index.js:112:9
at \node_modules\jsonwebtoken\index.js:98:18
at process._tickCallback (node.js:355:11)</pre>
以下是我的angularApp.js
文件:
app.factory('photos', ['$http', 'auth', function($http, auth){
var o = { photos: [] };
o.create = function(photo) {
return $http.post('/photos', photo, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
o.photos.push(data);
}).error(function(a) {
console.log(a);
});
};
return o;
}]);
app.controller('MainCtrl', [
'$scope',
'photos',
'auth',
function($scope, photos, auth){
$scope.photos = photos.photos;
$scope.addPhoto = function(){
photos.create();
};
}]);
index.ejs
代码:
<form ng-submit="addPhoto()">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile">
<button type="submit" class="btn btn-primary">Post</button>
</form>
这里是routes\index.js
router.post('/photos', auth, function(req, res, next) {
var dirname = require('path').dirname(__dirname);
var filename = req.files.file.name;
var path = req.files.file.path;
var type = req.files.file.mimetype;
var read_stream = fs.createReadStream(dirname + '/' + path);
var conn = req.conn;
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var gfs = Grid(conn.db);
var writestream = gfs.createWriteStream({
filename: filename
});
read_stream.pipe(writestream);
photo.save(function(err, photo){
if(err){ return next(err); }
res.json(photo);
});
});
据我所知,代码破坏了 运行 o.create
函数,但我不确定为什么它无法找到 photo
,而这是函数参数之一.
使用您为 routes/index.js
发布的代码,行
photo.save(...
正在使用以前未在当前或任何父范围中定义的变量 photo
。