Javascript Mongoose模式错误:';类型';未定义的

Javascript Mongoose模式错误:';类型';未定义的,javascript,node.js,mongoose,discord.js,mongoose-schema,Javascript,Node.js,Mongoose,Discord.js,Mongoose Schema,我需要帮助解决这个问题。 如果我将2或大于2的$guarantedTier放入我的数据库中,我会收到此错误“无法设置未定义的属性'type'。 但是如果在$guarantedTier中1,它会正常工作。 错误在第112行:cardToAdd.type='character'; 我能做些什么来解决这个问题 我的模式: const { model, Schema } = require('mongoose'); const Probability = require('probability-nod

我需要帮助解决这个问题。 如果我将2大于2$guarantedTier放入我的数据库中,我会收到此错误“无法设置未定义的属性'type'。 但是如果在$guarantedTier1,它会正常工作。 错误在第112行:cardToAdd.type='character'; 我能做些什么来解决这个问题

我的模式:

const { model, Schema } = require('mongoose');
const Probability = require('probability-node');
const _ = require('lodash');

const cardPackSchema = new Schema({
    name: {
        type: String,
        unique: true
    },
    description: String,
    cards: Number, //Número de cartas dentro desse pack
    type: String, //Se é um Personagem, Feitiço, Item, Todos
    tier: Number,
    probability: {
        //Probabilidade de pegar cartas de especifico tier
        1: Number,
        2: Number,
        3: Number,
        4: Number,
        5: Number,
    },
    price: Number,
    discount: Number, //Desconto em porcentagem %
    stock: Number, // -1 é infinito no estoque
    guaranteedTier: Number //Se refere ao tier da descriçao do pack
});

cardPackSchema.methods.purchase = async function(memberID) {
    //->Initialization
    const profile = await this.model('Profile')
      .findOne({ memberID })
      .exec();
  
    //->Validation
    if (this.stock === 0)
      return {
        res: 'err',
        title: 'Not available',
        desc: 'The pack you are trying to purchase is out of stock'
      };
  
    if (profile.coins < this.price - this.price * this.discount)
      return {
        res: 'err',
        title: 'Insufficient Coins',
        desc: 'You do not have sufficient coins to purchase this pack'
      };
  
    //->Purchasing
    const cards = []; //Cards to give to the player;
  
    //-> Adding Guaranteed tier card
    if (this.guaranteedTier > 1) {
      addCard.call(this, this.guaranteedTier);
      this.cards--;
    }
  
    //->Probabilitized function
    const addRandomCard = new Probability(
      {
        p: this.probability['1'],
        f: () => addCard.call(this, 1)
      },
      {
        p: this.probability['2'],
        f: () => addCard.call(this, 2)
      },
      {
        p: this.probability['3'],
        f: () => addCard.call(this, 3)
      },
      {
        p: this.probability['4'],
        f: () => addCard.call(this, 4)
      },
      {
        p: this.probability['5'],
        f: () => addCard.call(this, 5)
      }
    );
  
    //-> Adding the rest of the cards
    for (var i = 1 + cards.length; i <= this.cards; i++) {
      await addRandomCard();
    }
  
    //->Add Cards to inventory
    const deck = await this.model('Deck')
      .findOne({ memberID })
      .exec();
  
    profile.deductCoins(this.price - this.price * this.discount);
  
    await deck.addCards(cards);
    //->Return card names
    return {
      res: 'success',
      cards,
      coins: profile.coins - (this.price - this.price * this.discount)
    };
  
    //->Add Card function
    async function addCard(tier) {
      const randomTypeCard = Math.floor(Math.random() * 1) + 1;
  
      //Adding a Character card
      if (
        (randomTypeCard === 1 && this.type === 'all') ||
        this.type === 'character'
      ) {
        const cardToAdd = _.sample(await model('Character').find({ tier }));
        cardToAdd.type = 'character';
        if (
          !_.includes(cards.map(card => card.name), cardToAdd.name) ||
          cardToAdd.sold >= cardToAdd.stock
        ) {
          await cardToAdd.sell();
          cards.push(cardToAdd);
        } else addCard.call(this, tier);
      }
    }


};

model('CardPack', cardPackSchema);
const{model,Schema}=require('mongoose');
常量概率=要求(‘概率节点’);
const=require('lodash');
const cardPackSchema=新模式({
姓名:{
类型:字符串,
独一无二:真的
},
描述:字符串,
卡片:编号://Número de cartas dentro desse pack
类型:String,//Seéum Personagem,Feitiço,Item,Todos
级别:编号,
概率:{
//特殊层的可能性
1:号码,,
2:号码,,
3:号码,,
4:号码,,
5:号码,,
},
价格:数字,
折扣:数字,//DESCON em PROCENTAGEM%
股票:编号,//-1é无穷大无estoque
担保人:编号//Se referee ao tier da descripçao do pack
});
cardPackSchema.methods.purchase=异步函数(memberID){
//->初始化
const profile=wait this.model('profile'))
.findOne({memberID})
.exec();
//->验证
如果(this.stock==0)
返回{
res:'呃',
标题:“不可用”,
描述:“您试图购买的包装已售罄”
};
if(profile.coins购买
const cards=[];//给玩家的牌;
//->添加保证层卡
如果(this.guarantedTier>1){
addCard.call(这个,这个,担保人);
这张卡片;
}
//->概率化函数
const addRandomCard=新概率(
{
p:这个,概率['1'],
f:()=>addCard.call(这个,1)
},
{
p:这个,概率['2'],
f:()=>addCard.call(这个,2)
},
{
p:这个,概率['3'],
f:()=>addCard.call(这个,3)
},
{
p:这个,概率['4'],
f:()=>addCard.call(这个,4)
},
{
p:这个,概率[5'],
f:()=>addCard.call(这个,5)
}
);
//->添加其余的卡片
对于(var i=1+cards.length;我将卡片添加到库存
const deck=等待此模型('deck'))
.findOne({memberID})
.exec();
配置文件.扣除硬币(this.price-this.price*this.折扣);
等待牌组。添加牌(牌);
//->返回卡名
返回{
res:“成功”,
卡,
硬币:profile.coins-(this.price-this.price*this.折扣)
};
//->添加卡片功能
异步函数添加卡(层){
const randomTypeCard=Math.floor(Math.random()*1)+1;
//添加字符卡
如果(
(randomTypeCard===1&&this.type==='all')||
this.type=='character'
) {
const cardToAdd=u0.sample(等待模型('Character').find({tier}));
cardToAdd.type='character';
如果(
!\包括(cards.map(card=>card.name)、cardToAdd.name)||
cardToAdd.salled>=cardToAdd.stock
) {
等待卡片添加。出售();
卡片。推送(卡片添加);
}else addCard.call(本层);
}
}
};
型号(“CardPack”,cardPackSchema);

我认为问题在于您的数据库中没有所请求层(>=2)的
字符
文档

等待模型('Character')。查找({tier})
返回空数组和
。.sample([])==undefined


插入对该类型案例的检查,就可以了。

您是否阅读错误?这是我的字符模式:它与模式无关。只是数据库的实际内容。数据库中缺少$type:'Character'也是如此?我假设您必须检查
cardToAdd
之后是否有空响应
const cardToAdd=\usample(wait model('Character').find({tier}));
。它可以(事实上)通过查询
不返回任何文档。find({tier})