filter:post.edit, setTopicFields keeps forgetting data on restart



  • I'm having a frustrating time with setTopicFields, but only on filter:post.edit. filter:post.create works fine, and the code is pretty similar so I'm not sure what I'm missing. At first, it looks like it works properly, but when I restart NodeBB the data reverts back to how it was on post.create.

    The post data works correctly, but it creates a discrepancy where the post content says the right thing and the custom topicField says something out of date.

    plugin.createPostTruth = function (payload, callback) {
    
        topics.getTopicFields(payload.data.tid, ['truths'], function (err, topicData) {
    
            if (err) return console.log(err);
    
            var topicTruths = topicData.truths;
    
            var parsed = parseColored(payload.post.content);
    
            var truthpost = {
                'pid': payload.post.pid,
                'uid': payload.post.uid,
                'truths': parsed
            };
    
            if (truthpost.truths) {
    
                if (topicTruths && Array.isArray(topicTruths)) {
                    topicTruths.push(truthpost);
    
                } else {
                    topicTruths = [truthpost];
                }
            }
    
            topics.setTopicFields(payload.data.tid, {
                'truths': topicTruths
    
            });
        });
    
        callback(null, payload);
    }
    
    plugin.editPostTruth = function (payload, callback) {
    
        posts.getPostFields(payload.data.pid, ['tid'], function (err, postData) {
            topics.getTopicFields(postData.tid, ['truths'], function (err, topicData) {
    
                if (err) return console.log(err);
    
                var topicTruths = topicData.truths;
    
                var parsed = parseColored(payload.post.content);
    
                var truthpost = {
                    'pid': payload.post.pid,
                    'uid': payload.post.uid,
                    'truths': parsed
                };
    
                if (!truthpost.truths) {
                    console.log("checking for pid " + truthpost.pid);
                    var i = topicTruths.findIndex(function (o) {
                        return o.pid == truthpost.pid;
                    });
                    if (i >= 0) {
                        console.log("removing truths");
                        topicTruths.splice(i, 1);
                    }
                    //else return payload;
                } else {
    
                    if (topicTruths && Array.isArray(topicTruths)) {
    
                        console.log("checking to find pid " + truthpost.pid);
                        var i = topicTruths.findIndex(function (o) {
                            return o.pid == truthpost.pid;
                        });
                        if (i >= 0) {
                            console.log("Found pid. splicing truth");
                            topicTruths.splice(i, 1, truthpost);
    
                        } else {
                            topicTruths.push(truthpost);
                            console.log("No pid found. adding truth to bottom of stack.");
                        }
                    } else {
                        topicTruths = [truthpost];
                        console.log("There were no truths at all. making this the first truth...");
                    }
                }
    
                topics.setTopicFields(payload.data.tid, {
                    'truths': topicTruths
    
                });
            });
        });
        callback(null, payload);
    }
    
    

    The topic json before edit, and after restart:

     "title": "Example Topic",
        "uid": 1,
        "viewcount": 1,
        "truths": [
            {
                "pid": 136,
                "uid": 1,
                "truths": [
                    {
                        "type": "red",
                        "text": "This is a red."
                    }
                ]
            },
            {
                "pid": 137,
                "uid": 1,
                "truths": [
                    {
                        "type": "blue",
                        "text": "This is a blue."
                    }
                ]
            }
        ],
    

    The topic json just after edit, and what it should look like:

        "title": "Example Topic",
        "uid": 1,
        "viewcount": 1,
        "truths": [
            {
                "pid": 136,
                "uid": 1,
                "truths": [
                    {
                        "type": "red",
                        "text": "This is a red."
                    }
                ]
            },
            {
                "pid": 137,
                "uid": 1,
                "truths": [
                    {
                        "type": "gold",
                        "text": "This is not a blue."
                    }
                ]
            }
        ],
    

Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 7
  • 4
  • 3
| |