Object.assign 当 post 请求时复制

Object.assign make duplicate when post a request

我使用 zendesk 创建了一个工单,但我不知道为什么会这样

这里是节点js代码:

config.js

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

创建票证api

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 };
}

每当我 post 请求时,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,求助

反转 Object.assing 中的参数顺序。

你有

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

但应该是

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

Object.assign语法

Object.assign(target, ...sources)

你的情况

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

的原始数组对象的引用