猫鼬虚拟计数字段:访问时,this.populate()。execPopulate()字段为null,除非首先在填充的文档上调用.toJSON()

时间:2018-12-29 01:00:44

标签: mongoose mongoose-populate

我希望发生的事情:

在通过this(分配给populate().execPopulate())或async/awaitpopulatedDoc.then(populatedDoc => )填充this之后:

  • 记录populatedDocthis.fieldName会显示该字段及其正确值
  • 记录populatedDoc.fieldNamethis.toJSON().fieldName返回正确的值
  • 记录populatedDoc.toJSON().fieldNamethis返回正确的值

实际发生的事情:

在通过populate().execPopulate()(分配给async/await)或populatedDoc.then(populatedDoc => )this填充populatedDoc之后:

  • 记录this.fieldNamepopulatedDoc.fieldName会显示该字段及其正确值
  • 登录nullthis.toJSON().fieldName返回populatedDoc.toJSON().fieldName
  • 如果我登录const storySchema = new mongoose.Schema({ title: String, body: String, publishedDate: { type: Date, default: null, }, published: { // true: published, false: draft type: Boolean, default: false, }, author: { type: mongoose.SchemaTypes.ObjectId, ref: 'users' }, parent: { type: mongoose.SchemaTypes.ObjectId, ref: 'stories' }, }, { timestamps: true }); storySchema.virtual('repliesCount', { ref: 'stories', // collection name this field references localField: '_id', // the ID to of this story foreignField: 'parent', // the field on the Story document to match with the ID count: true, // only return a count of documents }); const Story = mongoose.model('stories', storySchema); repliesCount,则返回正确的值

模式(简体)

storySchema.methods.test = async function test() {
  this.populate('repliesCount')
    .execPopulate()
    .then((updatedDoc) => {
      console.log(this); // repliesCount field: 1
      console.log(this.repliesCount); // null
      console.log(this.toJSON().repliesCount); // 1

      console.log(updatedDoc); // repliesCount field: 1
      console.log(updatedDoc.repliesCount); // null ??
      console.log(updatedDoc.toJSON().repliesCount); // 1
    });


  const populatedDoc = await this.populate('repliesCount').execPopulate();
  console.log(populatedDoc); // repliesCount field: 1
  console.log(populatedDoc.repliesCount); // null ??
  console.log(populatedDoc.toJSON().repliesCount); // 1
}

实例方法

然后有一种使用console.log models/story.js:146 { publishedDate: 2018-12-29T00:37:46.267Z, published: true, _id: 5c26c1da02fb5f2303f071e0, author: { followers: [], following: [], _id: 5c26c1da02fb5f2303f071de, username: 'linnea', avatarURL: 'https://s3.amazonaws.com/uifaces/faces/twitter/yigitpinarbasi/128.jpg', createdAt: 2018-12-29T00:37:46.145Z, updatedAt: 2018-12-29T00:37:46.145Z, __v: 0 }, title: 'Multi-tiered mobile moratorium', body: 'Ut est laborum iure facilis. Voluptate dolores id accusamus. Delectus itaque qui harum occaecati. Consequatur deserunt harum repellendus est ut.\n \rDolorem in nostrum. Quae accusamus tempore eum. Vel sequi ipsam cupiditate excepturi iusto quis. Quam voluptatum aperiam laudantium sit eveniet nisi deserunt cumque. Qui architecto libero aut ipsa quae est saepe ipsam.', parent: null, createdAt: 2018-12-29T00:37:46.230Z, updatedAt: 2018-12-29T00:37:46.268Z, __v: 0, repliesCount: 1 } console.log models/story.js:147 null console.log models/story.js:148 1 字段的方法。我以方法为例进行了简化。我尝试同时使用async / await和传统的.then():

$nameTshirt = 'T-Shirt NDOE';
$priceTshirt = 35.00;
$nameAlbum = 'Thousands of Scars Album';
$size = $_GET['size'];
$quantity = $_GET['quantityOne'];

$stmt = mysqli_prepare($conn, "INSERT INTO shoppingCart(name, size, quantity, price) 
    VALUES (?, ?, ?, ?);") or die(mysqli_error($conn);
mysqli_stmt_bind_param($stmt, "ssid", $nameTshirt, $size, $quantity, $priceTshirt);
mysqli_stmt_execute($stmt) or die(mysqli_stmt_error($stmt);

实例方法输出日志

{{1}}

0 个答案:

没有答案