gpt4 book ai didi

javascript - NodeJs : Reference Error : (my own var) not defined

转载 作者:太空宇宙 更新时间:2023-11-04 03:09:18 25 4
gpt4 key购买 nike

尝试要求并使用我的路由器文件中的模块。我已经设法要求它(我认为),但现在它显示以下错误:

ReferenceError: USERS is not defined at c:\work\nodejs\router\main.js:32:19 at Layer.handle [as handle_request] (c:\work\nodejs\node_modules\express\lib\router\layer.js:82:5) at next (c:\work\nodejs\node_modules\express\lib\router\route.js:110:13) at Route.dispatch (c:\work\nodejs\node_modules\express\lib\router\route.js:91:3) at Layer.handle [as handle_request] (c:\work\nodejs\node_modules\express\lib\router\layer.js:82:5) at c:\work\nodejs\node_modules\express\lib\router\index.js:267:22 at Function.proto.process_params (c:\work\nodejs\node_modules\express\lib\router\index.js:321:12) at next (c:\work\nodejs\node_modules\express\lib\router\index.js:261:10) at c:\work\nodejs\node_modules\body-parser\lib\read.js:111:5 at IncomingMessage.onEnd (c:\work\nodejs\node_modules\body-parser\node_modules\raw-body\index.js:136:7)

其中 USERS 是一个包含对象数组的对象,如下所示:

//users.js
module.exports = function () {

// Create User prototype and objects
var USERS = { users: [] };

function User(type, useremail, password) {
this.type = type;
this.useremail = useremail;
this.password = password;
}

var Bob = new User("rep", "bob@bob.com", "qwerty");
USERS.users.push(Bob);

var Helen = new User("rep", "helen@helen.com", "test");
USERS.users.push(Helen);

var Dominic = new User("customer", "dom@dom.com", "1234");
USERS.users.push(Dominic);

var James = new User("grower", "james@james.com", "pass1");
USERS.users.push(James);

};

我的路由器代码是:

var url = require('url'); var users = require('./router/users.js');

module.exports = function (app) {

app.get('/', function (req, res) {
res.render('index.html');
console.log("Home page displayed");
});

app.get('/login', function (req, res) {
res.render('login.html');
console.log("Log in page displayed");

});

app.get('/api/orders/:id', function (req, res) {
console.log(req.params.id);
res.json(ORDER.orders[req.params.id]);
});

app.get('/api/orders/', function (req, res) {
console.log(ORDER);
res.json(ORDER);

}); app.get('/api/users/:id', function (req, res) {
console.log(req.params.id);
res.json(USERS.users[req.params.id]);
});

app.get('/api/users/', function (req, res) {
console.log(USERS);
res.json(USERS);

});

在这种情况下,如何在我的路由器中正确引用用户?

最佳答案

//users.js


// Create User prototype and objects
var USERS = { users: [] };

function User(type, useremail, password) {
this.type = type;
this.useremail = useremail;
this.password = password;
}

var Bob = new User("rep", "bob@bob.com", "qwerty");
USERS.users.push(Bob);

var Helen = new User("rep", "helen@helen.com", "test");
USERS.users.push(Helen);

var Dominic = new User("customer", "dom@dom.com", "1234");
USERS.users.push(Dominic);

var James = new User("grower", "james@james.com", "pass1");
USERS.users.push(James);

};

module.exports = USERS

或者更好:

var getUsers = function(){ return Users.users;};
var addUser = function(name, email, somethingelse)
{
var u = new User(name, email, somethingeelse);
Users.users.push(u);
};

module.exports = {
getUsers: getUsers,
addUser: addUser
}

关于javascript - NodeJs : Reference Error : (my own var) not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28669457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com