Mongoose:无法通过ID检索文档 - 错误404

时间:2018-04-02 12:43:29

标签: javascript mongodb express mongoose server

我正在尝试使用mongoose从MongoDB中检索对象,但是我收到错误404.

router.get('/blogs/:id', function(req, res){
console.log('getting one blog post by id');
Blog.findOne({
   _id: req.params.id
})
   .exec(function (err, blog) {
       if(err){
           res.send('error occured');
       } else{
           console.log(blog);
           res.render('entry', {entries: entry});
       }
   })
});

错误消息:** 5abe5efa06ac64917363277a无法加载资源:服务器响应状态为404(未找到)。**

文件存在于数据库中:

{
    "_id" : ObjectId("5abe5efa06ac64917363277a"),
    "title" : "this is the first blog",
    "author" : "me",
    "body" : "this is a post",
    "comments" : [ 
        ObjectId("5ac1fe92f2eb490c3c5b1357")
    ]
}

这是我的entry.ejs视图:

<% include header %>
<div class="panel panel-default">

    <div class="panel-heading">

        <div class="text-muted pull-right">
            <%= entry.published %>
        </div>
        <%= entry.author  %>  <a class="btn btn-default" href="blogs/<%= entry._id %>"> <%= entry.title %></a>
    </div>
    <div class="panel-body">
        <%= entry.body %>
        <div id="comments">

            <% entry.comments.forEach(function(comment){ %>
            <%= comment.commentAuthor + " : " + comment.comm %>
            <% }) %>
        </div>
        <div>

        </div>
    </div>

    <div class="panel-body">

        <a class="btn btn-default" href="<%= entry._id %>/new-comment">Add new comment</a>

    </div>
</div>

以下是我定义架构的方式:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var exports = module.exports = {};

exports.commentSchema = new Schema({
    commentAuthor: String,
    comm: String,
    date: { type: Date, default: Date.now }
});

exports.blogSchema = new Schema({
    title:  String,
    author: String,
    body:   String,
    comments: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'commentSchema' }],
    date: { type: Date, default: Date.now },

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

使用以下代码,我可以检索所有博客:

var express = require('express');
var router = express.Router();
var schema = require('../model/schema');

/* GET users listing. */
router.get('/', function(req, res, next) {
    schema.Blog.find({}, function (err, blogs) {
        if (err) {
            console.log(err);

        } else{
            res.render('blogs', {entries: blogs});
        }

    });
});


module.exports = router;

1 个答案:

答案 0 :(得分:1)

由于您要向<x:stylesheet version="1.0" xmlns:x="http://www.w3.org/1999/XSL/Transform"> <x:template match="/"> <fo:root text-align="center" font-family="Arial" font-size="9pt"> <fo:layout-master-set> <fo:simple-page-master master-name="m" margin-left="7mm" margin-right="9mm" margin-top="8mm" margin-bottom="1mm" page-width="210mm" page-height="297mm"> <fo:region-body margin-top="14mm" margin-left="2mm"/> <fo:region-before /> <fo:region-after /> </fo:simple-page-master> </fo:layout-master-set> <x:apply-templates select="root/item-abc"/> <x:apply-templates select="root/item-def"/> <x:apply-templates select="root/item-ghj"/> </fo:root> </x:template> <x:template match="root/item-abc"> <fo:page-sequence master-reference="m" format="00001"> .... </fo:page-sequence> </x:template> ...another items' templates </x:stylesheet> 发送string,而需要ObjectId ,您才会收到错误。

尝试以下,

解决方案1:

query

解决方案2:

    import mongoose from 'mongoose';
    (or)
    var mongoose = require('mongoose');

    router.get('/blogs/:id', function(req, res){
    console.log('getting one blog post by id');
    Blog.findOne({
       _id: mongoose.Types.ObjectId(req.params.id)
    })
       .exec(function (err, blog) {
           if(err){
               res.send('error occured');
           } else{
               console.log(blog);
               res.render('entry', {entries: entry});
           }
       })
    });