Search code examples
javascriptnode.jsecmascript-6ecmascript-5

Exporting map function into other file


Is it possible to export the function inside the orders.map which is 'order' and import it on the other JS file with function inside? I am getting an error order is not defined. Thank you

main.js

     const getReport = async function() {
      const jsonlUrl = 'https://next.json-generator.com/api/json/get/xxxxx'
      const res = await fetch(jsonlUrl);
      const orders = await res.json();

      const orderlines = orders.map(order => ({

       order_id: order.order_id,

      customer_name: getName()

      }));

---

function.js

const getName = function() {
const customerName = order.customer_name
 return customerName
}


Solution

  • Resolved: Passed the parameter into function call

    main.js
         const getReport = async function() {
          const jsonlUrl = 'https://next.json-generator.com/api/json/get/xxxxx'
          const res = await fetch(jsonlUrl);
          const orders = await res.json();
    
          const orderlines = orders.map(order => ({
    
           order_id: order.order_id,
    
          customer_name: getName(order)
    
          }));
    
    

    I added the parameter on the function getName

    function.js
    const getName = function(order) { 
    const customerName = order.customer_name 
    return customerName 
    }