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
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
Same goes for your errors. I'm sure you will figure it out after some time, but don't expect to be able to run code, figure stuff out without trouble, that isn't the point. The point is being presented a problem and being able to figure it out over time. Google your way out of your error, bash your head against the code, and learn through trial error and understanding. That is how you get experience

that it periodically fetches data from the original server, say once every 2 hours and then caches it

The netlify server instance shouldn't stay running longer than a few seconds, or minutes after each call between calls. Thus you will need something running "long term" if you want to cache it for 2 hours

amateur_dev
@amateur-dev
Hello Fellow Campers. I am looking for FE Campers to join a project on which I am working on. I recently participated in a hackathon and WON in it. Now I am being requested to develop it further and superfast. Will be glad to get any of the Fellow Campers to test out the skills that we have acquired through FCC onto a real life product. Here is a link to the blog post mentioning about my team wining the hackathon https://blog.kyber.network/kyberdefi-hackathon-meet-the-winners-bea5bc9ec983 . Drop me a dm if you would like to come work on this.
Tingclub
@Tingclub
有人能帮忙解读一下这个代码么https://codepen.io/freeCodeCamp/full/YqLyXB