Node version: v10.15.2
Sails version _(sails)_: 1.0.2
DB adapter & version _(e.g. [email protected])_: MongoDB shell version v3.4.3
In my controller, the fn function executes return.suucess() before my variables are properly defined. In this case, the ytb object. The sails.log of "items" is correct, but "ytb" did not return.
I also tried with Promise.then, and .all. Also, with async.each() and removing the await inside this function.
fn: async function (inputs, exits) {
var url = require('url');
var items = await Collection.find({});
_.each(items, async item => {
if (item.youtube_url) {
var ytbUrl = item.youtube_url;
var regexBaseUrlYtb = new RegExp('(https://www.youtube.com/channel/)');
if (regexBaseUrlYtb.test(ytbUrl)) {
var ytbIdChannel = ytbUrl.replace(regexBaseUrlYtb, '');
var ytbChannel = await sails.helpers.youtubeApi(ytbIdChannel);
item.ytb = {};
Object.assign(item.ytb, ytbChannel);
}
}
sails.log('items: ', item)
});
return exits.success({
items
});
}
I expect that the ytb object will also be passed with the object item at the view, but the items object is passed without the ytb object.
Also, in general, I expect the return.success() runs only once the function finished
Thanks.
@BenArthuys Thanks for posting! We'll take a look as soon as possible.
In the mean time, there are a few ways you can help speed things along:
Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.
For help with questions about Sails, click here.
Should artist.ytb = {}; read item.ytb = {};?
Should
artist.ytb = {};readitem.ytb = {};?
Sorry I've edited this. just a mistake.
@BenArthuys -
I believe a semicolon is needed after sails.log('items: ', item).
It may be worthwhile to check if everything is looking defined and matching on the Collection side as well.
Interesting YouTube controller 馃憤
I think that the return is firing immediately because you likely need to Promise.all
```
Promise.all(
items.map(async item => {
if (item.youtube_url) {
var ytbUrl = item.youtube_url;
var regexBaseUrlYtb = new RegExp('(https://www.youtube.com/channel/)');
if (regexBaseUrlYtb.test(ytbUrl)) {
var ytbIdChannel = ytbUrl.replace(regexBaseUrlYtb, '');
var ytbChannel = await sails.helpers.youtubeApi(ytbIdChannel);
item.ytb = {};
Object.assign(item.ytb, ytbChannel);
}
}
sails.log('items: ', item)
});
)
Hey, @BenArthuys! I believe that _.each takes a synchronous function, which might be why you're not getting the asynchronous behavior you expected. Does the following implementation fix the problem?
fn: async function (inputs, exits) {
var url = require('url');
var items = await Collection.find({});
for(let item of items) {
if (item.youtube_url) {
var ytbUrl = item.youtube_url;
var regexBaseUrlYtb = new RegExp('(https://www.youtube.com/channel/)');
if (regexBaseUrlYtb.test(ytbUrl)) {
var ytbIdChannel = ytbUrl.replace(regexBaseUrlYtb, '');
var ytbChannel = await sails.helpers.youtubeApi(ytbIdChannel);
item.ytb = {};
Object.assign(item.ytb, ytbChannel);
}
}
sails.log('items: ', item)
};
return exits.success({
items
});
}
@madisonhicks Thanks so much! it fix the problem
Most helpful comment
I think that the return is firing immediately because you likely need to Promise.all
```
Promise.all(
items.map(async item => {
if (item.youtube_url) {
var ytbUrl = item.youtube_url;
var regexBaseUrlYtb = new RegExp('(https://www.youtube.com/channel/)');
});
)