节点。js - express3-handlebars和mongoose。

时间:2022-03-31 18:28:06

i'm stuck on what i know is probably a super easy issue for more experienced node.js developers to solve. i can't seem to get my returned query to display in my handlebars template.

我坚持我所知道的可能是一个非常容易的问题,对于更有经验的节点。js开发人员解决。我似乎无法让返回的查询显示在我的handlebars模板中。

i have my app setup like so:

我的应用程序设置如下:

app.js

app.js

var express  = require('express')
  , mongoose = require('mongoose')
  , exphbs   = require('express3-handlebars');

var uri = 'mongodb://localhost/test';

global.db = mongoose.connect(uri);

var app = express();

// configure Express
app.configure(function() {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'handlebars'); //set to handlebars to use the handlebars engine
  app.engine('handlebars', exphbs({defaultLayout: 'main'})); //set to express3-handlebars to use handlebars
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use('/', express.static('public'));
  app.use(app.router);
});

var api = require('./public/js/api');

app.get('/', function (req, res) {
    res.render('home', {user: api.user});
});

app.listen(3000);

console.log('Listening on port 3000');

I then have my schema in a model folder and it is named userschema.js

然后在模型文件夹中有我的模式,它被命名为userschema.js。

userschema.js

userschema.js

var Schema = require('mongoose').Schema;

//create our user model
var userSchema = Schema({
  username: { type: String, required: true, unique: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true},
});

// db is global
module.exports = db.model('users', userSchema);

i am then exposing the model through a js file called api.js

然后,我将通过一个名为api.js的js文件公开这个模型。

var model = require('../../models/userschema')

exports.modelName = function (req, res) {
  res.send('my model name is ' + model.modelName);
}

exports.user = function(req, res) {
    model.findOne({username: 'TestUser'}, function (err, user) {
        //res.send(user);
        res.render('home', {user: user.username});
        console.log('USER:' + user.username);
  });
}

i don't understand why i can't get this to display on my handlebars template. if i change api.user to 'ME' then i see output, if i don't i get an error. also if i go app.get('/', api.user); i see my query returned. any suggestions would be appreciated.

我不明白为什么我不能把它显示在我的把手模板上。如果我改变api。用户对“我”,然后我看到输出,如果我不我得到一个错误。如果我去app.get('/', api.user);我看到我的查询返回了。如有任何建议,我们将不胜感激。

1 个解决方案

#1


0  

Well, you've already stated one possible solution, which is to bind api.user to a route such as '/':

您已经声明了一个可能的解决方案,那就是绑定api。用户的路线,如'/':

also if i go app.get('/', api.user); i see my query returned.

如果我去app.get('/', api.user);我看到我的查询返回了。

The issue is that api.user is a function -- an apparent route handler:

问题在于api。用户是一个函数——一个明显的路由处理程序:

exports.user = function(req, res) {
    model.findOne({username: 'TestUser'}, function (err, user) {
        //res.send(user);
        res.render('home', {user: user.username});
        console.log('USER:' + user.username);
  });
}

But, you seem to be trying to use it as though it's a value from your userSchema:

但是,你似乎在尝试使用它,好像它是你的userSchema的一个值:

app.get('/', function (req, res) {
    res.render('home', {user: api.user});
});

I'm guessing the error you're getting is because Handlebars is trying to execute the function as a getter. But, under this use, the 2nd argument, res, won't be an object with a render() method.

我猜你得到的错误是由于Handlebars试图执行作为getter的函数。但是,在此使用下,第2个参数,res,将不会是使用render()方法的对象。

#1


0  

Well, you've already stated one possible solution, which is to bind api.user to a route such as '/':

您已经声明了一个可能的解决方案,那就是绑定api。用户的路线,如'/':

also if i go app.get('/', api.user); i see my query returned.

如果我去app.get('/', api.user);我看到我的查询返回了。

The issue is that api.user is a function -- an apparent route handler:

问题在于api。用户是一个函数——一个明显的路由处理程序:

exports.user = function(req, res) {
    model.findOne({username: 'TestUser'}, function (err, user) {
        //res.send(user);
        res.render('home', {user: user.username});
        console.log('USER:' + user.username);
  });
}

But, you seem to be trying to use it as though it's a value from your userSchema:

但是,你似乎在尝试使用它,好像它是你的userSchema的一个值:

app.get('/', function (req, res) {
    res.render('home', {user: api.user});
});

I'm guessing the error you're getting is because Handlebars is trying to execute the function as a getter. But, under this use, the 2nd argument, res, won't be an object with a render() method.

我猜你得到的错误是由于Handlebars试图执行作为getter的函数。但是,在此使用下,第2个参数,res,将不会是使用render()方法的对象。