【问题标题】:Printing Object doesnt contain all keys打印对象不包含所有键
【发布时间】:2020-02-27 18:50:29
【问题描述】:

我完全迷路了。这是我用来打印对象的特定键的一些测试代码,然后我打印整个对象。

console.log(docs[0].mc_ign);
console.log(docs[0]);

现在这是我在控制台上看到的输出:

The__TxT
{
  id: 0,
  status: 1,
  testing: false,
  _id: 5dbc17eb20b3a8594d569570,
  timestamp: 2019-11-01T11:32:59.380Z,
  mc_uuid: 'dac89e44d1024f3b810478ed62d209a1',
  discord_id: '653029505457586176',
  email_address: 'gut97930@eveav.com',
  country: 'Germany',
  birth_month: 3,
  birth_year: 1943,
  about_me: 'about me text',
  motivation: 'motivation text',
  build_images: '',
  publish_about_me: true,
  publish_age: false,
  publish_country: true,
  __v: 0
}

mc_ign 键在哪里?

对象本身来自mongoose,缺失的key是我事后添加的:

docs[i].mc_ign = mc_ign;

我尝试在添加键和分配值之前和之后记录整个对象。他们都是一样的。 我错过了什么?为什么我可以读出值,但看不到?

【问题讨论】:

  • 也许不是问题,但重要的是要注意:像 chrome 这样的浏览器会实时更新对象,所以在你转储它的那一刻,值可能就在那里,但后来它会改变。要获取当时对象的真实快照,请使用console.log(JSON.parse(JSON.stringify(docs[0])));

标签: javascript node.js object mongoose


【解决方案1】:

它是猫鼬文档对象。要实现您想要的,请执行以下操作。

docs[0] = docs[0].toObject();
docs[0].mc_ign = "stuff";
console.log(docs[0])

.toObject() 将其转换为纯 JS 对象。

【讨论】:

    猜你喜欢
    • 2014-12-11
    • 2020-05-24
    • 2020-05-02
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多