【问题标题】:Removing subdocument in mongoose在猫鼬中删除子文档
【发布时间】:2019-06-06 13:51:32
【问题描述】:

我有一个这样的模型:

 const Screen = new Schema(
    {
        userId: [
            {
                type: ObjectId,
                default: null
            }
        ],
        contents: [
            {
                content: {
                    type: ObjectId,
                    ref: "Content",
                    default: null
                }
            }
        ],
    },
    { strict: false }
);

我想删除内容 subcdoc 中的内容,我是这样写的:

screenModel
    .find({ screenCode: screenCode })
    .lean()
    .exec()
    .then(screen =>
    {
        const newScreen = screen;
        newScreen.contents.id(_id).remove();
        screenModel.replaceOne(screen, newScreen);
        return newScreen;
    })

但它返回给我的错误:

“错误:无法读取未定义的属性 'id'
在 APIError.ExtendableError (D:\Projelerim\React\adonis-api-v1\workspace\dist\api\helpers\APIError.js:35:11)
在新的 APIError (D:\Projelerim\React\adonis-api-v1\workspace\dist\api\helpers\APIError.js:63:101)
在 D:\Projelerim\React\adonis-api-v1\workspace\dist\config\express.js:129:20
在 Layer.handle_error (D:\Projelerim\React\adonis-api-v1\workspace\
ode_modules\express\lib\router\layer.js:71:5)
在 trim_prefix (D:\Projelerim\React\adonis-api-v1\workspace\
ode_modules\express\lib\router\index.js:315:13)
在 D:\Projelerim\React\adonis-api-v1\workspace\
ode_modules\express\lib\router\index.js:284:7
在 Function.process_params (D:\Projelerim\React\adonis-api-v1\workspace\
ode_modules\express\lib\router\index.js:335:12)
在下一个 (D:\Projelerim\React\adonis-api-v1\workspace\
ode_modules\express\lib\router\index.js:275:10)
在 D:\Projelerim\React\adonis-api-v1\workspace\
ode_modules\express\lib\router\index.js:635:15
在 Immediate.next (D:\Projelerim\React\adonis-api-v1\workspace\
ode_modules\express\lib\router\index.js:260:14)
在 Immediate._onImmediate (D:\Projelerim\React\adonis-api-v1\workspace\
ode_modules\express\lib\router\index.js:635:15)
在 runCallback (timers.js:706:11)
在 tryOnImmediate (timers.js:676:5)
在 processImmediate (timers.js:658:5)
在 process.topLevelDomainCallback (domain.js:121:23)

架构如下所示:

"contents":[  
  {  
     "content":{  
        "$oid":"5c38a45c7bb5f232d4c3233c"
     },
     "_id":{  
        "$oid":"5c38ad1a994d6b436826778d"
     }
  },
  {  
     "content":{  
        "$oid":"5c38a45c7bb5f232d4c3233c"
     },
     "_id":{  
        "$oid":"5c38ad3d994d6b436826778e"
     }
  }

]

这段代码有什么问题?我尝试将 id() 更改为 pull() 并且它也给出了错误请帮助我:(

【问题讨论】:

    标签: node.js mongodb express mongoose subdocument


    【解决方案1】:

    我希望你知道精益的工作原理.lean()

    启用了精简选项的查询返回的文档是纯 JavaScript 对象,而不是 MongooseDocuments。他们没有应用保存方法、getter/setter 或其他 Mongoose 魔法。


    调试你的错误,

    无法读取未定义的属性“id”

    显然,它在这一刻崩溃了newScreen.contents.id
    请添加console.log() 以验证then 中收到的screen 是否是您要查找的内容。

    恕我直言,screenArray,其中包含您的屏幕,因此,您必须对其进行迭代。
    像这样:

    for (const sc of screen) { ...your logic with removing }
    

    【讨论】:

    • 我使用了 console.log() 并找到了屏幕。还删除了lea() 函数,它不会再次更改,给出错误“未定义的id 属性”
    • 同样 consol.log() 给出这样的内容: contents: [ [Object], [Object] ], __v: 0 } ]
    • @MuratÇELİK 如您所见,contents 中没有id,要使其正常工作,您必须有这样的结果contents: { id: 'foo' }。现在你的内容有数组了,当然代码找不到id属性
    • @MuratÇELİK 也许您正在寻找contents 中的数据,因此,例如,您可以在contents[0].id 这样的内容中查找id。但是,我不知道您的架构是什么样的
    • 我添加的架构看起来像在数据库中。是的,当 console.log(JSON.stringify(screen.contents)) 时,它是未定义的。它无法提取我认为的内容...
    猜你喜欢
    • 2021-07-28
    • 1970-01-01
    • 2016-07-30
    • 2016-11-09
    • 1970-01-01
    • 2021-05-16
    • 2019-11-19
    • 2020-06-10
    • 2022-11-24
    相关资源
    最近更新 更多