Update Topic slug

NodeBB Development
  • @Developers

    What's the right way to update a topic slug?
    I am trying to "parse and convert" all topic titles, but I would like the slugs to match the changes.

    basically something like this

       var parse = function() { /* some custom parser function ...*/  };
       var slugify = utils.slugify; // the core NodeBB utils.slugify
    
       var newTitle = parse(oldTitle);
    
        db.setObjectField('topic:' + topic.tid, 'title', newTitle, function(err) {
            if (err) return cb(err);
            db.setObjectField('topic:' + topic.tid, 'slug', slugify(newTitle), cb);
        });
    

    This results in some weird behavior, the topic generated URLs no longer include the tid so

    /topic/1/my-topic-title changed to /topic/my-topic-title

    Screen Shot 2014-10-15 at 6.57.07 PM.png

    I am assuming there is another field that needs to be updated, but can't find it 😕

    relevant issue: https://github.com/akhoury/nodebb-plugin-import/issues/45, last few comments are the most relevant.

  • The topic slugs include the topic id so the value stored in the database for this topic is 2647/update-topic-slug for example.

    Your code should be

    db.setObjectField('topic:' + topic.tid, 'title', newTitle, function(err) {
        if (err) return cb(err);
        db.setObjectField('topic:' + topic.tid, 'slug', topic.tid + '/' + slugify(newTitle), cb);
    });
    
  • ah crap, i totally missed reading this line!

    thanks you @baris


Suggested Topics