Javascript 无法返回值以响应mongoose/mongodb和nodejs
我通过Mongoose使用Nodejs、ExpressJs和MongoDB。我创建了一个简单的用户模式。我将代码分为多个文件,因为我预见到它们会变得越来越复杂 url“/api/users”被配置为调用“routes/user.js”中的list函数,这将按预期进行。UserSchema的list函数确实被调用,但它无法向调用函数返回任何内容,因此没有结果 我做错了什么 我试着根据 我认为我在userSchema.statics.list的函数定义上做错了什么 app.jsJavascript 无法返回值以响应mongoose/mongodb和nodejs,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我通过Mongoose使用Nodejs、ExpressJs和MongoDB。我创建了一个简单的用户模式。我将代码分为多个文件,因为我预见到它们会变得越来越复杂 url“/api/users”被配置为调用“routes/user.js”中的list函数,这将按预期进行。UserSchema的list函数确实被调用,但它无法向调用函数返回任何内容,因此没有结果 我做错了什么 我试着根据 我认为我在userSchema.statics.list的函数定义上做错了什么 app.js users_modu
users_module = require('./custom_modules/users.js'); // I have separated the actual DB code into another file
mongoose.connect('mongodb:// ******************');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
users_module.init_users();
});
app.get('/api/users', user.list);
自定义_模块/users.js
function init_users() {
userSchema = mongoose.Schema({
usernamename: String,
hash: String,
});
userSchema.statics.list = function () {
this.find(function (err, users) {
if (!err) {
console.log("Got some data"); // this gets printed
return users; // the result remains the same if I replace this with return "hello"
} else {
return console.log(err);
}
});
}
UserModel = mongoose.model('User', userSchema);
} // end of init_users
exports.init_users = init_users;
exports.list = function (req, res) {
UserModel.list(function (users) {
// this code never gets executed
console.log("Yay ");
return res.json(users);
});
}
路由/user.js
function init_users() {
userSchema = mongoose.Schema({
usernamename: String,
hash: String,
});
userSchema.statics.list = function () {
this.find(function (err, users) {
if (!err) {
console.log("Got some data"); // this gets printed
return users; // the result remains the same if I replace this with return "hello"
} else {
return console.log(err);
}
});
}
UserModel = mongoose.model('User', userSchema);
} // end of init_users
exports.init_users = init_users;
exports.list = function (req, res) {
UserModel.list(function (users) {
// this code never gets executed
console.log("Yay ");
return res.json(users);
});
}
实际上,在您的代码中,您传递的是一个回调,在函数
userSchema.statics.list
您可以尝试以下代码:
userSchema.statics.list = function (calbck) {
this.find(function (err, users) {
if (!err) {
calbck(null, users); // this is firing the call back and first parameter should be always error object (according to guidelines). Here no error, so pass null (we can't skip)
} else {
return calbck(err, null); //here no result. But error object. (Here second parameter is optional if skipped by default it will be undefined in callback function)
}
});
}
因此,您应该更改传递给此函数的回调。i、 e
exports.list = function (req, res){
UserModel.list(function(err, users) {
if(err) {return console.log(err);}
return res.json(users);
});
}
有错误吗?是的。行。我今天学到了很多新东西。我一直以为回拨会自己打电话。到目前为止,我使用的函数已经在其定义中定义了回调。所以我只是利用了它们,并假设每个函数都可以有一个回调,并且回调在函数完成后自动执行。