【问题标题】:the if statement is not working inspite of it's value being true while used with an mongoose object in node尽管在节点中与猫鼬对象一起使用时 if 语句的值为 true,但 if 语句不起作用
【发布时间】:2021-10-15 09:19:33
【问题描述】:

所以我正在创建一个竞价应用程序, 这是投标模型的架构

const bidSchema = new mongoose.Schema({
    
    name: String,
    price : Number,
    description: String,
    location: String,
    specilization: String,
    image: String,
    createdUser: {
      type: mongoose.Schema.Types.ObjectId,
      ref: User
    },
    highestBidder: {
      highBidderName: {
        type: mongoose.Schema.Types.ObjectId,
        ref: User
      },
      highPrice: Number,
      
     },
     previousBidders: [{previousName: {
      type: mongoose.Schema.Types.ObjectId,
      ref: User
     } , previousPrice: Number}],
     isClosed: {
       type: Boolean,
       enum:[true]
     }
     
})

这是我的出价路线,将在其中添加出价

route.post('/:id/submitbid',isloggedin, async (req,res) => {
    
    const {id} = req.params

const bids = await Bid.findById(id)

if(! bids.highestBidder){

    bids.highestBidder.highBidderName = req.user._id
    bids.highestBidder.highPrice = req.body.highPrice
   await bids.save()
   console.log('if worked')
    res.redirect(`bids/${bids._id}`)
    

} else {

   const previousName = bids.highestBidder.highBidderName
   const previousPrice = bids.highestBidder.highPrice

   bids.highestBidder.highBidderName = req.user._id
   bids.highestBidder.highPrice = req.body.highPrice
    
   bids.previousBidders.push({previousName,previousPrice})
   console.log('else worked')
   await bids.save()
   res.redirect(`/bids/${bids._id}`)

}

})

最初,bids.highestBidder 部分不包含任何内容, 因此,当我在执行 /:id/submit 路由之前控制台记录出价对象时,这就是我在控制台中得到的。

{
  _id: new ObjectId("616944da40c3b2ac3779f93b"),
  name: 'gratus',
  price: 10,
  description: ';fdja;dfja;ldf;a',
  location: 'india',
  image: 'https://source.unsplash.com/random/200x200?sig=1',
  previousBidders: [],
  createdUser: {
    _id: new ObjectId("61687ab4a79b1fa356b9fca5"),
    email: 'gratus@gratus.com',
    username: 'gratus',
    __v: 0
  },
  __v: 0

可以看到没有bids.highestBidder存在,所以当我执行/:id/submitbid post路由时,if语句应该执行,但这是我在执行该路由后在控制台中得到的

else worked

我不知道为什么 else 工作, 问题是,如果我现在控制台记录了我的出价对象,这就是我得到的

else worked
{
  highestBidder: {
    highBidderName: {
      _id: new ObjectId("61687ab4a79b1fa356b9fca5"),
      email: 'gratus@gratus.com',
      username: 'gratus',
      __v: 0
    },
    highPrice: 5
  },
  _id: new ObjectId("616944da40c3b2ac3779f93b"),
  name: 'gratus',
  price: 10,
  description: ';fdja;dfja;ldf;a',
  location: 'india',
  image: 'https://source.unsplash.com/random/200x200?sig=1',
  previousBidders: [ { _id: new ObjectId("616944e740c3b2ac3779f943") } ],
  createdUser: {
    _id: new ObjectId("61687ab4a79b1fa356b9fca5"),
    email: 'gratus@gratus.com',
    username: 'gratus',
    __v: 0
  },
  __v: 1
}

它正在创建一个空的 previousBidders 数组,这会破坏我的整个应用程序。 我真的很感激任何解决方案。

【问题讨论】:

  • 您的 if 语句不执行,因为它不正确。如果 bids.highestBidder 不存在,则不能这样做:bids.highestBidder.highBidderName = req.user._id。你应该这样做:bids.highestBidder = {highBidderName : req.user._id}

标签: javascript node.js mongodb express mongoose


【解决方案1】:

Mongoose 模型实例不是普通对象。它们通常具有特殊的方法来生成您在执行 console.log 时看到的 json 输出。这意味着该字段很可能存在,但由于未设置其子属性,因此根本不会在控制台输出中呈现。

由于highestBidder 具有子属性,您正在执行的默认布尔if-check 可能由于这些属性而返回true。您可以通过以下方式避免这种情况:

if ( !bids.highestBidder || !bids.highestBidder.highBidderName ) {
 ...
}

这样做的目的是,如果未声明highestBidder,它会立即跳转到执行if 中的代码。否则,它还将检查highestBidderName 属性是否有值,如果没有找到则执行if 代码块。这样可以避免 mongoose 添加的幽灵子属性影响您的条件结果。

可能有比这更优雅的解决方案,但这是一种快速简便的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2012-07-16
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多