【问题标题】:meteor update method not working流星更新方法不起作用
【发布时间】:2017-04-18 07:07:54
【问题描述】:

这是我的收藏:

{
    "_id" : "Kan6btPXwNiF84j8e",
    "title" : "Chapter Title 1",
    "businessId" : "qmWJ3HtZrpka8dpbM",
    "createdBy" : "GfdPfoPTiSwLv8TBR",
    "sections" : [ 
        {
            "id" : "T5KAfTcCb7pCudT3a",
            "type" : "TEXT",
            "data" : {
                "text" : "<h1>2</h1><h1>asd</h1>"
            },
            "createdAt" : ISODate("2016-12-03T10:35:59.023Z"),
            "updatedAt" : ISODate("2016-12-03T10:35:59.023Z")
        }
    ],
    "createdAt" : ISODate("2016-12-02T12:15:16.577Z"),
    "updatedAt" : ISODate("2016-12-03T12:54:50.591Z")
}

这是我从客户端调用的流星方法

deleteSection: function (section_id, chapter_id) {


        chaptersCollection.update(
            {$and: [{_id: chapter_id}, {'sections.id': section_id}]},
            {$pull: {'sections': {'id': section_id}}},
            function (err, numAffected) {
                if (err) {
                    console.log(err);
                    return err;
                }else{
                    console.log(numAffected);
                }
            });
        return 'Section Successfully Deleted';
    }

在meteor方法的回调函数中,它返回1作为受影响的行。但是服务器上的文档没有更新。

有什么建议我哪里错了吗?

【问题讨论】:

    标签: meteor meteor-collections


    【解决方案1】:

    我在项目中尝试使用 pull 时遇到了类似的问题。因此,我没有使用 $pull,而是在数据库外部处理数组,然后将数组设置为我在外部处理的数组。所以也许你可以尝试类似的方法作为替代方法

    deleteSection: function (section_id, chapter_id){
    
         const oldArray = chaptersCollection.findOne(chapter_id).sections;
         const newArray = oldArray.filter(function(section) {
                             return section.id !== section_id
                             });
    
         chaptersCollection.update({_id: chapter_id},
                                   {$set: {sections: newArray}},
                                   function (err, numAffected) {
                                       if (err) {
                                           console.log(err);
                                           return err;
                                        }else{
                                           console.log(numAffected);
                                        }
                                    });
         return 'Section Successfully Deleted';
    
    }
    

    【讨论】:

    • 这是实现我想要的替代方法,不能解决问题。谢谢你的答案。 @CagriYardimci
    【解决方案2】:

    你真的需要$and吗?

    deleteSection: function (section_id, chapter_id) {
        chaptersCollection.update(
            {_id: chapter_id, 'sections.id': section_id},
            {$pull: {'sections': {'id': section_id}}},
            function (err) {
                if (err) {
                    console.log(err);
                    return err;
                }else{
                    console.log('success');
                    return 'success';
                }
            });
    }
    

    【讨论】:

    • 没关系,我试过不工作@HafizAllyLalani
    猜你喜欢
    • 2015-09-23
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多