1

当我运行以下内容时,我得到

$ node t.js 
{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}
{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}
{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}

并且time没有添加键/值。

有人能弄清楚为什么吗?

t.js

const toml = require('./toml');
const moment = require('moment');

const t = toml('non-production.toml');

let a = new Object;
a = t.Jira.pack.c;
console.log(a);

const time = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ");
a["customfield_11904"] = time;
a.customfield_11904 = time;
console.log(a);

t.Jira.pack.c.customfield_11904 = time;
console.log(t.Jira.pack.c);

toml.js

const TOML = require('@iarna/toml');
const fs = require('fs');

module.exports = (filename) => {
  return TOML.parse(fs.readFileSync(filename, 'utf-8'));
}

非生产.toml

[Jira]
  pack.c = '{ "project": { "key": "DEMO" }, "issuetype": { "id": 10002 }, "priority": { "id": "3" }}'
4

1 回答 1

2

在您的情况下,a仍然是一个字符串。因此,您无法向其添加新密钥。
首先将其解析为 JSON:

const toml = require('./toml');
const moment = require('moment');

const t = toml('non-production.toml');

let a = JSON.parse(t.Jira.pack.c);
console.log(a);

const time = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ");
a.customfield_11904 = time;

console.log(a);

这现在产生

{
  project: { key: 'DEMO' },
  issuetype: { id: 10002 },
  priority: { id: '3' }
}
{
  project: { key: 'DEMO' },
  issuetype: { id: 10002 },
  priority: { id: '3' },
  customfield_11904: '2021-03-12T09:39:15.259+0100'
}
于 2021-03-12T08:36:38.107 回答