2017-06-29 7 views
0

私は3つのコレクションから適切な応答を得ようとしています(私は単純化してコレクションを変更できません)。 2番目のpopulate()で2番目の()を使わないと正しい結果が得られますが、私は3番目のコレクションから情報を取得するためにこれを行う必要があります。私は間違って何をしていますか?私の誤りはどこですか?それを修正するには?私のコレクションから2番目のマングースのポピュレートが原因で空のjson結果が返される

api.js

var express = require('express'); 
var app = express(); 
var bodyParser = require('body-parser'); 
var mongoose = require('mongoose'); 

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 

mongoose.connect('mongodb://localhost/books'); 
var db = mongoose.connection; 
db.on('connected', function() { 
    console.log('MongoDB connection successful'); 
}); 

Author = require('./models/books'); 
Book = require('./models/books'); 
Genre = require('./models/books'); 

app.post('/api/Books', function (req, res) { 
    var param = req.body.name; 

    Author.getAuthor({ name: param }, 10) 
     .then(populate_author => { 
      return Book.getBook({ id_a: populate_author.id_a }); 
     }) 
     .then(populate_books => { //this then() with populate() causes empty result 
      return Genre.getGenre({ id_g: populate_books.id_g }); 
     }) 
     .then(results => { 
      console.log(results); 
      res.json(results.map(result => ({ 
       id_a : result.populate_author.id_a, 
       name : result.populate_author.name, 
       gender : result.populate_author.gender, 
       title : result.title, 
       pub_date : result.pub_date, 
       id_g: result.populate_books.id_g, 
       genre : result.populate_books.genre, 
       description : result.populate_books.description 
      }))); 
     }) 
     .catch(error => { 
      console.log(error); 
     }); 
}); 

app.listen(3000); 
console.log('Server started and waits on port 3000'); 

books.js

var mongoose = require('mongoose'); 

var authorSchema = mongoose.Schema({ 
    id_a:{ 
     type: Number, 
     ref: 'books', 
     required: true 
    }, 
    name:{ 
     type: String, 
     required: true 
    }, 
    gender:{ 
     type: String, 
     required: true 
    }, 
    born:{ 
     type: Number, 
     required: true 
    }, 
    birthplace:{ 
     type: String, 
     required: true 
    }}, 
    { collection: 'author'} 
); 

var booksSchema = mongoose.Schema({ 
    id_b:{ 
     type: Number, 
     required: true 
    }, 
    title:{ 
     type: String, 
     required: true 
    }, 
    id_a:{ 
     type: Number, 
     required: true 

    }, 
    pub_date:{ 
     type: Number, 
     required: true 
    }, 
    publisher:{ 
     type: String, 
     required: true 
    }, 
    pages:{ 
     type: Number, 
     required: true 
    }, 
    id_g:{ 
     type: Number, 
     ref: 'genres', 
     required: true 
    }}, 
    { collection: 'books', toJSON: { virtuals: true }} 
); 

var genresSchema = mongoose.Schema({ 
    id_g:{ 
     type: Number, 
     required: true 
    }, 
    genre:{ 
     type: String, 
     required: true 
    }, 
    description:{ 
     type: String, 
     required: true 
    }}, 
    { collection: 'genres', toJSON: { virtuals: true }} 
); 

booksSchema.virtual('populate_author', { 
    ref: 'author', 
    localField: 'id_a', 
    foreignField: 'id_a', 
    justOne: true 
}); 

genresSchema.virtual('populate_books', { 
    ref: 'books', 
    localField: 'id_g', 
    foreignField: 'id_g', 
    justOne: true 
}); 

var Author = module.exports = mongoose.model('author', authorSchema); 
var Book = module.exports = mongoose.model('books', booksSchema); 
var Genre = module.exports = mongoose.model('genres', booksSchema); 

module.exports.getAuthor = function (query) { 
    return Author.findOne(query) 
    .exec(); 
} 

module.exports.getBook = function (query) { 
    return Book.find(query) 
    .populate('populate_author') 
    .exec(); 
} 

module.exports.getGenre = function (query) { 
    return Genre.find(query) 
    .populate('populate_books') 
    .exec(); 
} 

エキス:

著者

{"_id" : ObjectId("59492addd80eb0f9c1b42fd9"),"id_a" : 1,"name" : "Agatha Christie","gender" : "female","born" : 1890,"birthplace" : "England"} 

ブック

{"_id" : ObjectId("59492cd1d80eb0f9c1b42fda"),"id_b" : 1,"title" : "Murder on the Orient Express","id_a" : 1,"pub_date" : 1934,"publisher" : "Collins Crime Club","pages" : 256,"id_g" : 100} 

ジャンル

{"_id" : ObjectId("59553065062b59078d808c5e"),"id_g" : 100,"genre" : "crime","description" : "Crime fiction is the literary genre that..."} 

私のJSON-結果:だから

[] 
+0

実際には、結合を「エミュレート」するためのmongooseメソッドである '.populate()'を呼び出すことはありません。あなたは何をしようとしているのですか? 3つのコレクションの「結合された」結果を返しますか?訂正を指摘するには、コードに多すぎる問題があります。 '.find()'は、 "単一"の結果を探しているような "配列"を返します。 "結合"が必要な場合は、['$ lookup'](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/)が、代わりに探しているはずです。 –

+1

'populate_books'仮想フィールドを' book'スキーマに作成してから、 'populate_author'の後に' populate_books'フィールドに値を設定する必要があります – Mirodil

答えて

0

、私は私のquestiを解決しましたこの記事を読んだ後:https://medium.com/@thatisuday/deep-virtual-population-in-mongoose-is-actually-very-simple-45053531eea1。人口と仮想の仕組みを理解するのに役立ちました。また、いくつかの関数の名前を変更して、コードを書き換えなければならないことを理解した後でコードを書き直しました。この記事の前に私はそれが実際にどのように動作するのか分からなかった。

関連する問題