将地图功能导出到其他文件

时间:2020-04-26 12:20:54

标签: javascript node.js ecmascript-6 ecmascript-5

是否可以将orders.map中的函数导出为“ order”,并将其导入具有功能的另一个JS文件中?我收到未定义的错误订单。谢谢

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
}

1 个答案:

答案 0 :(得分:2)

已解决: 将参数传递给函数调用

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)

      }));

我在函数getName上添加了参数

function.js
const getName = function(order) { 
const customerName = order.customer_name 
return customerName 
} 
相关问题