使用 populate 获取参考文档,但它以 1 的数组形式返回完整文档
Using populate to get reference document, but it is returning the full document in an array of 1
每当我调用客户端时,它都会在客户端[0] 中显示完整的json 客户端文档。我希望能够调用 work.client.name 和 work.client.url 等。很抱歉,如果这是一个重复的问题,我搜索了很久也没有找到答案。
工作控制者
exports.work_detail = function(req, res, next) {
async.parallel({
work: function(callback) {
Work.findById(req.params.id)
.populate('client')
.exec(callback);
}
}, function(err, results) {
if (err) { return next(err); }
if (results.work==null) { // No results.
var err = new Error('Work not found');
err.status = 404;
return next(err);
}
// Successful, so render.
console.log(results.work.client.name);
res.render('work/work_detail', { title: 'Work Details', work: results.work } );
});
};
工作模式
var workSchema = new Schema({
client: [{ type: Schema.Types.ObjectId, ref: 'Client', required: true }],
time: {
type: Number,
},
work_done: {
type: Array,
},
work_value: {
type: [{}],
},
total: {
type: Number,
// set: calculateValue
},
work_address: {
type: String,
},
note: {
type: String,
}
}, {
timestamps: true
});
客户端模型
var clientSchema = new Schema({
first_name: {
type: String,
required: true,
set: firstToUpper
},
last_name: {
type: String,
required: true,
set: firstToUpper
},
address: {
type: String,
required: false,
},
phone: {
type: Number,
required: false,
},
email: {
type: String,
required: false,
lowercase: true
},
note: {
type: String,
required: false,
}}, {
timestamps: true,
toObject: { virtuals: true },
toJSON: { virtuals: true },
});
// Capitalize first letter of each word when called
function firstToUpper (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// Virtual for client's full name
clientSchema
.virtual('name')
.get(function () {
return this.first_name + ' ' + this.last_name;
});
// Virtual for clients's URL
clientSchema
.virtual('url')
.get(function () {
return '/invoice/client/' + this._id
});
模式被意外地包裹在数组中,由 Neil Lunn 指出
每当我调用客户端时,它都会在客户端[0] 中显示完整的json 客户端文档。我希望能够调用 work.client.name 和 work.client.url 等。很抱歉,如果这是一个重复的问题,我搜索了很久也没有找到答案。
工作控制者
exports.work_detail = function(req, res, next) {
async.parallel({
work: function(callback) {
Work.findById(req.params.id)
.populate('client')
.exec(callback);
}
}, function(err, results) {
if (err) { return next(err); }
if (results.work==null) { // No results.
var err = new Error('Work not found');
err.status = 404;
return next(err);
}
// Successful, so render.
console.log(results.work.client.name);
res.render('work/work_detail', { title: 'Work Details', work: results.work } );
});
};
工作模式
var workSchema = new Schema({
client: [{ type: Schema.Types.ObjectId, ref: 'Client', required: true }],
time: {
type: Number,
},
work_done: {
type: Array,
},
work_value: {
type: [{}],
},
total: {
type: Number,
// set: calculateValue
},
work_address: {
type: String,
},
note: {
type: String,
}
}, {
timestamps: true
});
客户端模型
var clientSchema = new Schema({
first_name: {
type: String,
required: true,
set: firstToUpper
},
last_name: {
type: String,
required: true,
set: firstToUpper
},
address: {
type: String,
required: false,
},
phone: {
type: Number,
required: false,
},
email: {
type: String,
required: false,
lowercase: true
},
note: {
type: String,
required: false,
}}, {
timestamps: true,
toObject: { virtuals: true },
toJSON: { virtuals: true },
});
// Capitalize first letter of each word when called
function firstToUpper (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// Virtual for client's full name
clientSchema
.virtual('name')
.get(function () {
return this.first_name + ' ' + this.last_name;
});
// Virtual for clients's URL
clientSchema
.virtual('url')
.get(function () {
return '/invoice/client/' + this._id
});
模式被意外地包裹在数组中,由 Neil Lunn 指出