Javascript 在express中调用函数-未定义
我正在用express编写后端javascript,但由于某些原因,调用时我的函数未定义。 看起来是这样的:Javascript 在express中调用函数-未定义,javascript,node.js,express,Javascript,Node.js,Express,我正在用express编写后端javascript,但由于某些原因,调用时我的函数未定义。 看起来是这样的: exports.check = function(req, res) { //check if username is in database var my_result = authenticator(req); //authenticator is defined below console.log(typeof(authenticator(req))); //ret
exports.check = function(req, res) { //check if username is in database
var my_result = authenticator(req); //authenticator is defined below
console.log(typeof(authenticator(req))); //returns undefined
};
function authenticator(req) {
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'users');
db.once('open', function callback() {
var personschema = mongoose.Schema({
username: String,
password: String
})
var person1 = db.model('personcollection', personschema)
person1.find({
username: req.body.username,
password: req.body.password
}, function(err, obj) {
if (obj.length === 0) {
return "yay";
} else {
return "neigh";
}
} //end function
function authenticator(req, callback) {
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost','users');
db.once('open', function() {
var personschema = mongoose.Schema({
username: String,
password: String
});
var person1 = db.model('personcollection',personschema)
person1.find({ username: req.body.username, password: req.body.password }, function(err, obj) {
// in this callback you do what you want with this result!
callback(obj.length === 0);
});
});
}
//快速路线如下所示:
exports.check = function(req, res) { //check if username is in database
var my_result = authenticator(req); //authenticator is defined below
console.log(typeof(authenticator(req))); //returns undefined
};
function authenticator(req) {
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'users');
db.once('open', function callback() {
var personschema = mongoose.Schema({
username: String,
password: String
})
var person1 = db.model('personcollection', personschema)
person1.find({
username: req.body.username,
password: req.body.password
}, function(err, obj) {
if (obj.length === 0) {
return "yay";
} else {
return "neigh";
}
} //end function
function authenticator(req, callback) {
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost','users');
db.once('open', function() {
var personschema = mongoose.Schema({
username: String,
password: String
});
var person1 = db.model('personcollection',personschema)
person1.find({ username: req.body.username, password: req.body.password }, function(err, obj) {
// in this callback you do what you want with this result!
callback(obj.length === 0);
});
});
}
当我把它放在express route中时,函数本身就可以工作,但是我想用尽可能少的代码保持路由的美观。这是一种选择吗
谢谢您的帮助。您正在尝试从异步函数返回值。句点您对node.js和异步编程有一个根本性的误解,需要阅读教程,了解异步代码以及为什么它们不能返回值,而必须使用回调、事件或承诺
您正在尝试从异步函数返回值。句点您对node.js和异步编程有一个根本性的误解,需要阅读教程,了解异步代码以及为什么它们不能返回值,而必须使用回调、事件或承诺
欢迎来到精彩的JavaScript异步世界: 甚至还有Node.js世界 这是因为节点中的任何网络都不能同步完成——这意味着您必须使用回调 您的验证器函数应该如下所示:
exports.check = function(req, res) { //check if username is in database
var my_result = authenticator(req); //authenticator is defined below
console.log(typeof(authenticator(req))); //returns undefined
};
function authenticator(req) {
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'users');
db.once('open', function callback() {
var personschema = mongoose.Schema({
username: String,
password: String
})
var person1 = db.model('personcollection', personschema)
person1.find({
username: req.body.username,
password: req.body.password
}, function(err, obj) {
if (obj.length === 0) {
return "yay";
} else {
return "neigh";
}
} //end function
function authenticator(req, callback) {
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost','users');
db.once('open', function() {
var personschema = mongoose.Schema({
username: String,
password: String
});
var person1 = db.model('personcollection',personschema)
person1.find({ username: req.body.username, password: req.body.password }, function(err, obj) {
// in this callback you do what you want with this result!
callback(obj.length === 0);
});
});
}
两个旁注:
保持数据库连接分离怎么样?这样,您将在每次请求时打开它。
您似乎将普通密码存储在数据库中,因为您正在将它们与请求中传递的密码进行比较:o您真的应该在数据库中加密它们!
欢迎来到精彩的JavaScript异步世界: 甚至还有Node.js世界 这是因为节点中的任何网络都不能同步完成——这意味着您必须使用回调 您的验证器函数应该如下所示:
exports.check = function(req, res) { //check if username is in database
var my_result = authenticator(req); //authenticator is defined below
console.log(typeof(authenticator(req))); //returns undefined
};
function authenticator(req) {
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'users');
db.once('open', function callback() {
var personschema = mongoose.Schema({
username: String,
password: String
})
var person1 = db.model('personcollection', personschema)
person1.find({
username: req.body.username,
password: req.body.password
}, function(err, obj) {
if (obj.length === 0) {
return "yay";
} else {
return "neigh";
}
} //end function
function authenticator(req, callback) {
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost','users');
db.once('open', function() {
var personschema = mongoose.Schema({
username: String,
password: String
});
var person1 = db.model('personcollection',personschema)
person1.find({ username: req.body.username, password: req.body.password }, function(err, obj) {
// in this callback you do what you want with this result!
callback(obj.length === 0);
});
});
}
两个旁注:
保持数据库连接分离怎么样?这样,您将在每次请求时打开它。
您似乎将普通密码存储在数据库中,因为您正在将它们与请求中传递的密码进行比较:o您真的应该在数据库中加密它们!
这不是您的代码感兴趣的部分。你在这个验证器函数中还做了什么吗?我认为这与此无关,因为当我将它放在路由中时,它确实起作用。我将添加函数的其余部分。是的,这是相关的:检查我的答案。这不是您的代码感兴趣的部分。你在这个验证器函数中还做了什么吗?我认为这与此无关,因为当我将它放在路由中时,它确实起作用。我将添加函数的其余部分。是的,这是相关的:检查我的答案。建议:要么让回调函数同时传递err和obj,要么在访问obj之前添加obj检查。它可能未定义或在此处为null。需要在db.once中重命名命名回调函数或传入参数。按原样,这将导致无限递归。建议:让回调函数同时传递err和obj,或者在访问obj之前添加对obj的检查。此处可能未定义或为null。需要重命名db.once中的命名回调函数或传入参数。按原样,这将导致无限递归。