I've been working on the following code for awhile now. I'm fairly new to Promises and asynchronous code, but I've read some very handy posts here and elsewhere describing them in the process of creating the code below.
The purpose is to create a function to detect text in an article where the writers inserted link tags, and properly replace them as links to another article with the same name if they exist. In this case, the text tag link to another article called articleName
would be written as <<articleName>>
-- I know the style <<texthere>>
can cause some issues, but the actual tags themselves can change. I just want the text replacement to properly return so I can pass the new string into the response to be rendered.
I am at a loss with what the issue is, here -- it just seems that at some point after linkedArticle
is passed, the promise chain is broken. I'm thinking that using a regexp match (I've used string.js's between
function as well, with the same result) does not work with Promise.resolve()
. I've gotten output such as just a string of the matchedArray
to undefined
. I may be overdoing the return
and the Promise.resolve()
, I'm not sure.
I am using Node.js and Mongoose. I've played around using native promises, async
, string.js
and underscore
. The code below uses async and string.js.
I've tidied up the code below as much as I can to post here. Apologies for the long argument passing in + tons of returns and resolves (it may very well be a reason for my problem?). It was one of the things I was doing to check where the chain could be breaking; I kept it for this post just to segregate things a bit to hopefully improve readability.
Article schema is straightforward with a string entry for an introduction, which is what I used just for testing purposes.
//Show article
var show = (req, res, next) => {
let id = req.params.id;
Article.findOne({_id : id}).populate('_author').then(function(article) {
if (!article) return res.render('404');
return Promise.resolve(replaceTextLinks(article.introduction)).then(function(text){
console.log("Returned text is" + text)
});
});
}
var replaceTextLinks = (text) => {
let regExp = /<<([a-zA-z0-9 ]+)>>/g;
console.log("Initial passed in text is: " + text)
return Promise.resolve(text.match(regExp)).then(function(matchArray){
console.log("Matched array is: " + matchArray);
async.each(matchArray, function(matchText){
console.log("Matched Text is: " + matchText);
return linkArticle(text, matchText);
});
}).catch(function(err){
console.log("Error encountered: %s", err);
});
}
var linkArticle = (text, matchText) => {
let regExp = /([a-zA-Z]+)/g;
return Promise.resolve(matchText.match(regExp)).then(function(linkedArticle){
console.log("Linked Article is: " + linkedArticle);
return Promise.resolve(matchArticle(text, matchText ,linkedArticle));
})
}
var matchArticle = (text, matchText, linkedArticle) => {
return Article.findOne({title:linkedArticle}).then(function(matchedArticle) {
console.log("matchedArticle is: " + matchedArticle);
if(matchedArticle) {
return Promise.resolve(replaceTextWithArticle(text, matchText, matchedArticle, linkedArticle));
}
})
}
var replaceTextWithArticle = (text, matchText, matchedArticle, linkedArticle) => {
console.log("Replacing initial text: " + text);
replacedText = '<a href=' + '/articles/view/' + matchedArticle._id + ">" + linkedArticle + "</a>"
return Promise.resolve(S(text).replaceAll(matchText, replacedText).s).then(function(newText){
console.log("Replaced text is: " + newText);
return Promise.resolve(newText);
})
}
Below is the console output when I run 'show'. There is an article elsewhere in the Article collection called Privacy
, which matches the tag passed in, <<Privacy>>
. Returned text being undefined right after LinkedArticle on the console is telling me the hiccup is somewhere there, but I could be wrong. It is consistently so even with articles with three tags.
Initial passed in text is: This article contains a tag, <<Privacy>>.
Matched array is: <<Privacy>>
Matched Text is: <<Privacy>>
Linked Article is: Privacy
Returned text is undefined
matchedArticle is: { _id: 5909f3efe8d05c4e3827c4d1,
title: 'Privacy',
_author: 5909f3eee8d05c4e3827c4d0,
category: 'Security',
introduction: 'Type your article\'s introduction here...',
contents: 'Type your article\'s content here...',
conclusion: 'Type your article\'s conclusion here...',
__v: 0,
published: false }
Replacing initial text: This article contains a tag, <<Privacy>>.
Replaced text is: This article contains a tag, <a href=/articles/view/5909f3efe8d05c4e3827c4d1>Privacy</a>.
Thank you very much for any assistance you can offer; I'm just at a loss where the promise chain seems to be breaking itself.
via Sammyfrw
No comments:
Post a Comment