为什么我在结果猫鼬中使用map后出现_doc

问题描述 投票:0回答:1

我尝试使用地图来获取结果

ProductModel.find()
.sort({ _id: -1 })
.then((products) => {

    console.log(products)

    products = products.map((product) => {
      return { ...product, price: toIdr(product.price) };
    });

    console.log(products)

    res.render("index", {
      title: "Home",
      products,
      path: req.path,
    });
  });

第一条日志:

[
  {
    _id: new ObjectId('65ffe52c31f86de770a5110c'),
    title: 'naruto',
    price: 32000,
    description: 'Vel corporis laboriosam. Fugit sed unde nulla nesciunt laudantium.',
    imageUrl: '/assets/book1.jpg',
    userId: new ObjectId('65ffe505f94c639905f4ba13'),
    __v: 0
  }
]

然后第二个日志,在结果数组中使用map之后,向我展示了一堆我不知道的值

[
  {
    '$__': InternalCache { activePaths: [StateMachine], skipId: true },
    '$isNew': false,
    _doc: {
      _id: new ObjectId('65ffe52c31f86de770a5110c'),
      title: 'naruto',
      price: 32000,
      description: 'Vel corporis laboriosam. Fugit sed unde nulla nesciunt laudantium.',
      imageUrl: '/assets/book1.jpg',
      userId: new ObjectId('65ffe505f94c639905f4ba13'),
      __v: 0
    },
    price: 'Rp 32.000,00'
  }
]

我正在尝试将每个产品的价格格式化为新数组,那么为什么我不能在第二个日志中获得这样的值

[
  {
    _id: new ObjectId('65ffe52c31f86de770a5110c'),
    title: 'naruto',
    price: 'Rp 32.000,00',
    description: 'Vel corporis laboriosam. Fugit sed unde nulla nesciunt laudantium.',
    imageUrl: '/assets/book1.jpg',
    userId: new ObjectId('65ffe505f94c639905f4ba13'),
    __v: 0
  }
]
javascript arrays response mongoose-schema mongoose-populate
1个回答
0
投票

扩展运算符(...)不仅复制文档的属性,还复制 Mongoose 用于管理文档状态的一些内部属性。 您可以在find()查询之前使用lean()方法。它告诉 Mongoose 返回纯 JavaScript 对象而不是 Mongoose 文档。

© www.soinside.com 2019 - 2024. All rights reserved.