在mongoose中使用嵌入式文档

时间:2017-04-15 18:05:40

标签: mongoose mongoose-schema



var mongoose = require('mongoose');

var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

var Comment = mongoose.model('Comment', new Schema({
    title     : String,
    body      : String,
    date      : Date
}))

var Post = mongoose.model('Post', new Schema({
    comments  : [Comment]
}))

module.exports.Comment = Comment
module.exports.Post = Post




遵循一个简单应用程序的教程,在尝试创建其他内容并了解Mongoose模式时,我尝试使用嵌入式文档与先前应用程序定义模型的方式出错

我收到此错误

    throw new TypeError('Undefined type `' + name + '` at array `' + path +
    ^

TypeError:数组model

的未定义类型comments

1 个答案:

答案 0 :(得分:3)

要嵌入文档,您需要在引用文档中传递其架构。为此,您可以将模式作为中间步骤单独存储在变量中,然后使用它来定义模型。

        string userAnotherCard = Console.ReadLine();

        bool secondHand = true;
        secondHand = (userAnotherCard == "y");

        bool secondHandNo = true;
        secondHandNo = (userAnotherCard == "n");

        while (secondHand)
        {

            Hands.playerHand.Add(Deck.CardDeck[Hands.rando.Next(0, Deck.CardDeck.Length)]);
            Console.WriteLine("Your cards are: ");
            Hands.playerHand.ForEach(Console.WriteLine);

            Console.WriteLine("Would u like to take another card?");
            Console.ReadLine();

        }
            while (Hands.playerHand.Sum() > 21)
            {
                Console.WriteLine("You loss, your cards sum is more than 21");
                break;
            }


            while (secondHandNo)
                break;