【问题标题】:Nodejs, Mongoose and Jade get no data from DatabaseNodejs、Mongoose 和 Jade 没有从数据库中获取数据
【发布时间】:2014-08-12 00:02:03
【问题描述】:

我正在寻找我的问题,但我什至不知道问题出在哪里。

我得到了在我的路线中设置的标题,但没有来自数据库的数据...

我的模特:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.Types.ObjectId;


var blogSchema = new Schema({
  title: { type: String, required: true },
  author: { type: String, required: true },
  body: { type: String, required: true },
  date: { type: String, required: true },
  hidden: Boolean
});


module.exports = mongoose.model('Blog', blogSchema);

我的路由器:

var express = require('express'),
    Blog = require('../models/blog'),
    moment = require('moment');

moment.lang('de');

var router = express.Router();


router.get('/articles', function(req, res) {
    Blog.find(function(err, docs){
        return res.render('blog/articles', { 
            title: 'Blog_',
            articles: docs
        });
    });
}); 

app.use('/blog', router);

我的玉

extends ../layouts/default
include ../elements/form-elements

block content

    h1= title
    each article in articles
        .col-md-12
            div.title= article.title

我在页面上显示的唯一一个是

Blog_

那我做错了什么?

在错误文件中它只说:“无法读取未定义的属性'title'”

所以文章对象没有设置...但是为什么呢?

非常感谢

编辑 1:

将 article.title 更改为文章不会改变任何内容

在日志文件中是

GET /blog/articles HTTP/1.1 304 - - 3 ms

编辑 2:

似乎节点没有从数据库中获取任何数据... 是的,有一个测试数据集;)

console.log() ->

错误:空

文档:[]

解决方案发布为答案

【问题讨论】:

  • 你能把jade中的article.title改成article进行测试吗?我怀疑article 未定义,因此解释了您的错误消息。
  • 也试试console.log( docs )。我知道没有标准,但您在我希望的某个地方发布了.connect

标签: node.js mongodb express mongoose pug


【解决方案1】:

我知道这是一个非常古老的问题,它被 OP 标记为已回答,但我认为真正的问题出在“我的路由器”中,您没有引用您的“文档”(从数据库返回的数据)正确。请记住,“docs”是一个数组,因此您需要像这样引用它们:

router.get('/articles', function(req, res) {
    Blog.find(function(err, docs){
        return res.render('blog/articles', { 
            title: docs[0].title, // Get the title for first entry
            articles: docs[0].body // Get body for the first entry
        });
    });
});

我正在硬编码数组索引,但您可以使用循环从数组中获取每个项目。

我认为 OPs 解决方案无法解决问题,因为...

默认情况下,编译模型时:

const someModel = mongoose.model('someModel', SomeSchema);

mongoose 使用“someModel”名称创建一个集合,并在末尾添加一个“s”,因此如果您检查数据库,您的集合应该 显示为“someModels”。使用OP的解决方案:

{ collection: 'blog' }

作为创建博客架构时的第二个参数

var blogSchema = new Schema();

默认行为被覆盖,您的收藏名称将是您设置为收藏值的任何名称,在本例中为“博客”。

您可以在Mongoose official docs 中了解更多信息 或在MDN - Node/Express/Mongoose 的“模型”部分中

【讨论】:

    【解决方案2】:

    找到解决办法了……

    模型不对...

    var blogSchema = new Schema({
      title: { type: String, required: true },
      author: { type: String, required: true },
      body: { type: String, required: true },
      date: { type: String, required: true },
      hidden: Boolean
    }, {collection : 'blog'});
    

    必须在最后命名集合...因为它是用小写字母写的-.-

    真是假-永远不要再这样做了^^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-09
      • 2018-12-29
      • 2015-11-19
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      相关资源
      最近更新 更多