在通过this
(分配给populate().execPopulate()
)或async/await
或populatedDoc
向.then(populatedDoc => )
填充this
之后:
populatedDoc
或this.fieldName
会显示该字段及其正确值populatedDoc.fieldName
或this.toJSON().fieldName
返回正确的值populatedDoc.toJSON().fieldName
或this
返回正确的值在通过populate().execPopulate()
(分配给async/await
)或populatedDoc
或.then(populatedDoc => )
向this
填充populatedDoc
之后:
this.fieldName
或populatedDoc.fieldName
会显示该字段及其正确值null
或this.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}}