Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Aug 17 09:32
    User @bjorno43 unbanned @linkin-park
  • Jun 20 21:19
    @bjorno43 banned @shenerd140
  • Apr 25 17:13
    @mstellaluna banned @cmal
  • Jan 08 22:05
    @mstellaluna banned @dertiuss323
  • Nov 07 2018 04:42
    User @texas2010 unbanned @ows-ali
  • Nov 02 2018 16:25
    @texas2010 banned @ows-ali
  • Oct 12 2018 05:50
    @bjorno43 banned @NACH74
  • Oct 05 2018 23:02
    @mstellaluna banned @JomoPipi
  • Sep 16 2018 12:21
    @bjorno43 banned @yash-kedia
  • Sep 16 2018 12:16
    @bjorno43 banned @vnikifirov
  • Sep 05 2018 08:12
    User @bjorno43 unbanned @androuino
  • Sep 05 2018 07:38
    @bjorno43 banned @androuino
  • Aug 23 2018 16:57
    User @bjorno43 unbanned @rahuldkjain
  • Aug 23 2018 16:23
    @bjorno43 banned @rahuldkjain
  • Jul 29 2018 14:13
    User @bjorno43 unbanned @jkyereh
  • Jul 29 2018 01:00
    @bjorno43 banned @jkyereh
  • Jul 10 2018 22:09
    @bjorno43 banned @manafn
  • Jul 06 2018 15:20
    @texas2010 banned @imlegend19
  • Jul 03 2018 12:28
    @bjorno43 banned @vbvmatta
  • Jun 29 2018 13:54
    @bjorno43 banned @OGTechnoBoy
siliconchild
@siliconchild
the filter is returning undefined
the first console.log gives out the array
when i download the array and then run the filter on the browser console it works
i don't know why that is
Brad
@bradtaniguchi
your using forEach not filter, forEach returns nothing
siliconchild
@siliconchild
shit, i am so stupid. how did i not notice that
sorry :D
Brad
@bradtaniguchi
(this is why I use TypeScript :wink: )
siliconchild
@siliconchild
const fetchAllProducts = async () => {
    const categories = await GetActiveCategories();
    const categoriesIdArray = categories.map(category => category.CategoryId);
    return Promise.all(categoriesIdArray.map(categoryID => getSearchResults(' ', categoryID)));
 }

const hotDeals = async () => {
    const products = await fetchAllProducts();
    return products.filter(product => product.MRP/product.SellingPrice > .8)
}

const displayHotDeals = async () => {
    const data = await hotDeals();
    console.log('DATA',data)
}

displayHotDeals();
doing this is giving me an empty array whereas saving the products variable on browser console and then filtering works fine
Brad
@bradtaniguchi
I'd debug your code yourself, something as simple as your math being wrong would result in nothing coming back. I assume the network is returning the data as you expect too
siliconchild
@siliconchild
if its the math that's wrong, this wont work on the console right?
Brad
@bradtaniguchi
i have no idea what your doing in the console
like, are you even getting the data you expect? Add console.logs/debug statements in your code and find out
siliconchild
@siliconchild
I need to spread in the results from the Promise.All
it returns a nested array
Brad
@bradtaniguchi
nice :+1:
siliconchild
@siliconchild
const fetchAllProducts = async () => {
    const categories = await GetActiveCategories();
    const categoriesIdArray = categories.map(category => category.CategoryId);
    return Promise.all(categoriesIdArray.map(categoryID => getSearchResults(' ', categoryID)));
 }

const hotDeals = async () => {
    const productsNested = await fetchAllProducts();
    const products = productsNested.flat();
    return products.filter(product => product.DiscountPercentage > 15)
}

const displayHotDeals = async () => {
    const data = await hotDeals();
    console.log('DATA',data)
}

displayHotDeals();
is there a way to return a destructured array from the first function itself?
Kaz Baig
@kbaig
@siliconchild Promise.all returns a Promise so you could then or await it and destructure before returning
siliconchild
@siliconchild
@kbaig that works thank you
siliconchild
@siliconchild
Hello, do somebody have experience working with netlify lambda functions?
Brad
@bradtaniguchi
@siliconchild Thats pretty specific, is the issue you have specific to netlifiy's lambda function setup? or just "cloud functions" in general?
siliconchild
@siliconchild
well i am trying to invoke one lambda function inside another
i cant get that to work
Brad
@bradtaniguchi
That doesn't sound like the right way to go about things if netlify functions are the same as AWS and Googles
thats like trying to invoke an express function inside of an express function
siliconchild
@siliconchild
so the thing i want to do is like to chain cloud functions to make a combined function
Brad
@bradtaniguchi
so first why do you want to do such, and 2nd what are you getting right now that prevents it from working?
Why not call 1 netlify function and call normal functions in it?
and those normal functions can chain
siliconchild
@siliconchild
normal function as in function running in the frontend?
Brad
@bradtaniguchi
Like your writing JS right?
The only "netlify function" part that is strict is the execution environment and the exposed function you pass to exports.handler. Pass that just create other JS functions to call inside of that function, which should give what you want
siliconchild
@siliconchild
const fetchAllProducts = async () => {
    const categories = await GetActiveCategories();
    const categoriesIdArray = categories.map(category => category.CategoryId);
   const productsNested = await Promise.all(categoriesIdArray.map(categoryID => getSearchResults(' ', categoryID)));
   return ProductsNested.flat()
 }

const hotDeals = async () => {
    const products = await fetchAllProducts();
    return products.filter(product => product.DiscountPercentage > 15)
}

my lambda is to implement this functionality, the idea is to have one lambda that does a fetch of active categories, and then the next lambda get this data to calls the getSearchResults lambda to return a list of all products.

since we write normal javascript in a way that has seperate functions for each specific task, in a lambda are we supposed to not follow that pattern and write a big function that does everything in it?

Brad
@bradtaniguchi
no
to leverage lambda/cloud functions you just need to provide 1 function "publically" so netlify can call it, and pass in the arguments, past that it usually doesn't care except that you "respond" somehow. Just like express you can write whatever you want within the handler, how-ever you want to handle it
siliconchild
@siliconchild
@brad sorry i didn't understand that, this is what i tried doing
import fetch from 'node-fetch';
const {API_TOKEN} = process.env;

exports.handler = async (event,context,callback) => {

    const activeCategories = await fetch('/.netlify/functions/getActiveCategories')

    const categoriesIdArray = categories.map(category => category.CategoryId);
    const productsNested = await Promise.all(categoriesIdArray.map(categoryID => getSearchResults(' ', categoryID)));
    callback(null,{
        statusCode: 200,
        body: JSON.stringify(const productsNested)
    })
}

I also tried doing 

import getActiveCategories from './getActiveCategories';
and then 
const activeCategories = await getActiveCategories()
Brad
@bradtaniguchi
so what doesn't work with what your doing?
Oh your trying to make an http request to another function
you need to call it using fetch as if your calling it from the front-end. Or seperate out the logic in the getActiveCateogires to an actual function and cal it like your last example
but that only works if the server knows what function your talking about
siliconchild
@siliconchild
import fetch from 'node-fetch';
const {API_TOKEN} = process.env;

exports.handler = async (event,context,callback) => {

    const activeCategoriesRes = await fetch('https://api.example.com/category/GetActiveCategories',{
        method: 'GET',
        headers: {'APIToken': API_TOKEN}
    });
    const activeCategoriesData = await activeCategoriesRes.json();
    const categories = activeCategoriesData.result;
    const categoriesIdArray = categories.map(category => category.CategoryId);
    const productsNested = await Promise.all(categoriesIdArray.map(categoryID => {
        const categoryProductsRes = await fetch(`http://api.example.com/Product/GetProductSearchByCategory`,{
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'APIToken': API_TOKEN
        },
        body: JSON.stringify({
            "CategoryID": CategoryID,
            "Search": " ",
            "lstBrand": [],
            "lstColor": [],
            "lstSize": [],
            "PriceFrom": 0,
            "PriceTo": 0,
            "MaxPrice": 0,
            "Page": 1,
            "Show": 9999,
            "UserID": ""
        })
    })
    const categoryProductsData = await categoryProductsRes.json();
    return categoryProductsData.result;
    }));
    const productsList =  productsNested.flat();
    const hotDeals = productsList.filter(product => product.DiscountPercentage > 15 && product.SellingPrice > 50);

    callback(null,{
        statusCode: 200,
        body: JSON.stringify(hotDeals)
    })
}
siliconchild
@siliconchild
since i have separate lambdas that already do getActiveCategories and getSearchResults, this seems like duplication of code
Brad
@bradtaniguchi
You can share the code if you put the logic into a common function outside of the files. You will end up needing a build system tho
otherwise you will have to call the function using fetch correctly
I'd recommend reading into both approaches. The sharing code approach is more complex in terms of setting up, but will be more performant and scalable for development
siliconchild
@siliconchild
@bradtaniguchi sorry I'm not knowledgeable enough to understand the approaches you are referring to, could you send me link or something that i can read which describes the approaches you mentioned
Brad
@bradtaniguchi
@siliconchild So first of all, I point out stuff for you to go out and research yourself. Googling something as simple as "how to have multiple functions for a cloud function" is a good start. The other approach using fetch could be looked into as "how to use fetch in nodejs"
I don't suggest believing you need any more guidance than that most of the time. If you don't know what concepts i'm talking about then it sounds like its time to first figure out what I'm talking about (by getting more familiar in any way possible) and then learn how you can apply it in your own project