【问题标题】:Object.assign make duplicate when post a request发布请求时 Object.assign 重复
【发布时间】:2018-09-20 09:36:43
【问题描述】:

我使用 zendesk 创建了一张票,但我不知道为什么会发生这种情况

这里是节点js代码:

config.js

baseTicketObject: {
    'comment': {
      'body': null,
    },
    'requester': {
      'name': null,
      'email': null,
    },
    'custom_fields': [],
  },

创建票务接口

function createTicketObjectFromRequest(req) {
  const requestBody = req.body;
  console.log('requestBody', requestBody);
  console.log('config.baseTicketObject', config.baseTicketObject);
  const ticket = Object.assign(config.baseTicketObject, {});
  //console.log('ticket', ticket);
  const {
    messageBody, email, name, customFields,
  } = requestBody;
  //console.log('ticket.custom_fields', ticket.custom_fields);

  // Request must contain a name, email and body
  ticket.requester.name = name;
  ticket.requester.email = email;
  ticket.comment.body = messageBody;

  if (req.user && req.user.id) {
    ticket.custom_fields.push(createCustomFieldObject(config.customFieldNameToZendeskFieldIdMapping['userId'], Number(req.user.id)));
  }
  Object.keys(config.customFieldNameToZendeskFieldIdMapping).forEach((fieldName) => {
    if (config.customFieldNameToZendeskFieldIdMapping[fieldName] === config.customFieldNameToZendeskFieldIdMapping.userId) {
      return;
    }

    //console.log('fieldName', fieldName);
    const mappedCustomFieldId = config.customFieldNameToZendeskFieldIdMapping[fieldName];
    if (mappedCustomFieldId) {
      ticket.custom_fields.push(createCustomFieldObject(mappedCustomFieldId, !customFields[fieldName] ? '' : customFields[fieldName]));
    }
  });

  return { ticket: ticket };
}

每当我发布请求时,config.baseTicketObject 都会像这样保留我之前推送的所有项目

config.baseTicketObject { comment: { body: null },
  requester: { name: null, email: null },
  custom_fields: [] }
-------------------------------------
config.baseTicketObject { comment: { body: 'dgfhdgfhdgfh dgfhdfghdfg' },
  requester: { name: 'test other', email: 'tranthiphuonghue96@yopmail.com' },
  custom_fields:
   [ { id: 360010481051, value: '' },
     { id: 360010510411, value: '' },
     { id: 360010406792, value: '' },
     { id: 360010511011, value: '' },
     { id: 360010511191, value: '' },
     { id: 360010920852, value: 'contact_support' } ] }
---------------------------------------------------------
config.baseTicketObject { comment: { body: 'dgfhdgfhdgfh dgfhdfghdfg' },
  requester: { name: 'test other', email: 'tranthiphuonghue96@yopmail.com' },
  custom_fields:
   [ { id: 360010481051, value: '' },
     { id: 360010510411, value: '' },
     { id: 360010406792, value: '' },
     { id: 360010511011, value: '' },
     { id: 360010511191, value: '' },
     { id: 360010920852, value: 'contact_support' },
 { id: 360010481051, value: '' },
 { id: 360010510411, value: '' },
 { id: 360010406792, value: '' },
 { id: 360010511011, value: '' },
 { id: 360010511191, value: '' },
 { id: 360010920852, value: 'contact_support' } ] }

我不知道为什么config.baseTicketObject会这样,请帮忙。

【问题讨论】:

    标签: javascript node.js lodash zendesk


    【解决方案1】:

    Object.assing 中反转参数顺序。

    你有

    Object.assign(config.baseTicketObject, {});

    但应该是

    Object.assign({}, config.baseTicketObject);

    Object.assign 语法

    Object.assign(目标, ...来源)

    你的情况

    const ticket = Object.assign({}, config.baseTicketObject);
    

    编辑:

    添加

    ticket.custom_fields = [];

    之后

    const ticket = Object.assign({}, config.baseTicketObject);

    因为Object.assign 创建shallow copy,这意味着ticket.custom_fields 仍然持有来自config.baseTicketObject.custom_fields 的原始数组对象的引用

    【讨论】:

    • config.baseTicketobject 和 ticket 仍然是增量的,当我发布请求时它们不会刷新
    • 你能解释一下为什么会这样吗,我知道可以解决这个问题但不知道为什么
    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 2019-06-08
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 2018-07-04
    • 1970-01-01
    相关资源
    最近更新 更多