javascript Mongoose混合类型对象数组,可查找OneAndUpdate

eh57zj3b  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(96)

我有一个控制器编辑卡更新cardsArray对象的字段。cardsArray是混合类型对象,因为每个卡对象的字段不同,所以我存储混合。类型虽然推新卡使用addCard控制器工作完美,但当编辑卡控制器被调用时,它给出类型错误当编辑控制器被调用时给出以下错误:TypeError:无法读取null的属性(阅读“cardsArray”)

// Schema
     const userSchema = new mongoose.Schema(
              {
                name: {
                  type: String,
                  required: true,
                },
                email: {
                  type: String,
                  required: true,
                },
                password: {
                  type: String,
                  required: true,
                },
            
                cardsArray: [{ type: mongoose.Schema.Types.Mixed }],
              }

);

//__Mongodb Data
    
        {
          "_id": {
            "$oid": "63b43ab32fc8d3c100cafecc"
          },
          "name": "usern_name",
          "email": "pr****@gmail.com",
          "password": "$2b$12$3nwifHakrBu94BwLXAC4Nu16Kw0.xyW8vAIPTMSgY7cYttVklDIZq",
          "cardsArray": [
            {
              "title": "some_title",
              "category": "Bank",
              "cardHolder": "some_name",
              "cardNumber": "54545454",
              "expiry": "23/01",
              "cvv": "***",
              "logoIndex": 72,
              "isFavourite": false,
              "_id": {
                "$oid": "63b83cc77288d277ef359533"
              }
            }
          ],
          
          "loginIdsArray": [],
          "docsArray": [],
          "activitiesArray": [],
          "__v": 0
        }
    
    
    // Add Card Controller.js___
    
         addCard: async (req, res) => {
            console.log(req.body.data, req.body.user_id)
            // console.log('obj_id', newObjectId)
            req.body.data._id = newObjectId;
            try {
        
              const response = await UserDatabase.findOneAndUpdate(
                { _id: req.body.user_id },
                {
                  $push: {
                    // cardsArray: req.body.data,
                    cardsArray: { $each: [req.body.data] },
                  },
                },
                { returnOriginal: false }
              );
              res.status(200).send(response);
            } catch (error) {
              console.log(error)
              res.status(404).json({ message: error.message });
            }
          },
        
    // edit card controller.js 
 
editCard: async (req, res) => {
  const id = req.params.id;

  const { category, title, cardHolder, cardNumber, expiry, cvv, logoIndex, isFavourite } = req.body;
  console.log(req.params.id)

  try {
 

    const response = await UserDatabase.findOneAndUpdate(
      { _id: "63b43ab32fc8d3c100cafecc", 'cardsArray._id': "63b709fc69a1cfa6fccd645c" },
      {
        $set: {
          "cardsArray.$.title": req.body.title,
          "cardsArray.$.category": req.body.category,
          "cardsArray.$.cardHolder": req.body.cardHolder,
          "cardsArray.$.cardNumber": req.body.cardNumber,
          "cardsArray.$.expiry": req.body.expiry,
          "cardsArray.$.cvv": req.body.cvv,
          "cardsArray.$.logoIndex": req.body.logoIndex,
          "cardsArray.$.isFavourite": req.body.isFavourite
        }
      },
    );

    console.log(response)
    res.status(201).json(response.cardsArray);
  } catch (error) {
    console.log(error)
    res.status(404).json({ message: error.message });
  }
}
vsnjm48y

vsnjm48y1#

这意味着没有与following _id和cardsArray._id匹配的数据,我认为这样无法找到字段“cardsArray._id”,首先尝试通过findOne查找值

await UserDatabase.findOneAndUpdate(
  { _id: "63b43ab32fc8d3c100cafecc", 'cardsArray._id': "63b709fc69a1cfa6fccd645c" })

如果您的查找不起作用,请尝试以下方法。不确定,但可能会起作用,您必须查找id并循环使用$in或$match的cardsArray

await UserDatabase.findOneAndUpdate(
  { _id: "63b43ab32fc8d3c100cafecc", 'cardsArray':{$in:[ {_id:"63b709fc69a1cfa6fccd645c" }]})

相关问题