Tortilaman
Tortilaman

Reputation: 213

Running into issues with docBeforeSave

This might be a silly question, but I'm using self.docBeforeSave in a subclass of apostrophe-pieces in an attempt to generate a new property of a piece based on the rich text body of the piece. I'm able to retrieve everything just fine, but when I try and save it nothing goes to the database. Also, I'm now getting errors whenever it tries to save as well. I'm not using a property with an underscore. Code is as follows:

construct: function (self, options) {
    self.docBeforeSave = function (req, doc) {
        if (doc.type !== self.name) {
            return
        }
        var toc = [];
        var ind = 1;
        for (item of doc.body.items) {
            if (item.type == "apostrophe-rich-text") {
                var regexp = /<h[1-6]>(.*)<\/h[1-6]>/g;
                var headings = item.content.match(regexp);
                if (headings) {
                    for (heading of headings) {
                        var hOld = heading;
                        var hID = "sect-" + ind++;
                        var hCont = heading.replace(regexp, "$1");
                        var hNew = heading.replace(/(<h[1-6])/, "$1 id=\"" + hID + "\"");
                        item.content.replace(hOld, hNew);
                        var link = '<a href="#' + hID + '">' + hCont + '</a>';    
                        toc.push(link);
                    }
                }
            }
        }
        ind = 1;
        doc.contArr = toc;
        console.log(doc.contArr);
    }
}

Upvotes: 1

Views: 30

Answers (1)

Tom Boutell
Tom Boutell

Reputation: 7572

docBeforeSave must take req, doc, options (or req, doc, options, callback). It is misbehaving because you left off an argument and that's confusing the code that autodetects whether callbacks are desired.

Upvotes: 2

Related Questions