gpt4 book ai didi

javascript - 发布请求时 Object.assign 进行复制

转载 作者:行者123 更新时间:2023-12-03 01:05:33 26 4
gpt4 key购买 nike

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

这是 Node 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 };
}

每当我发布请求时,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

中原始数组对象的引用

关于javascript - 发布请求时 Object.assign 进行复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52421991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com