我是 MEAN 堆栈的新手,在如何从我的 javascript 中正确地将 ObjectId 与 MongoDB 链接时遇到了麻烦。然后,我还尝试获取与该 ObjectId 对应的名称以显示在我的视图中。
该代码旨在按计划进行约会,其中用户与每个约会相关联,然后视图显示有关每个约会的信息(即每个用户的姓名)。
这是我的计划架构:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ScheduleSchema = new Schema({
description: String,
category: [String],
location: String,
startDate: { type: Date, default: Date.now },
endDate: { type: Date, default: Date.now },
participants: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
author: {
type: Schema.Types.ObjectId,
ref: 'User'
},
timestamp: { type: Date, default: Date.now },
active: Boolean
});
module.exports = mongoose.model('Schedule', ScheduleSchema);
以及用户的架构:
var UserSchema = new Schema({
name: String,
email: { type: String, lowercase: true },
role: {
type: String,
default: 'user'
},
hashedPassword: String,
provider: String,
salt: String
});
该 ^ 文件还有很多内容,但我认为架构是唯一相关的部分。
这是我的控制器代码,它通过 2 个真实用户对象(参与者)传递虚假测试信息:
// Use our rest api to post a new meeting
$scope.addMeeting = function() {
$http.post('/api/schedules', { description: "Testing schedule adding", participants: ["547f03befccbd4f93874b547", "5481dcbf5dad7f735a766ad9"], category: "1", location: "Small Conference Room", startDate: new Date(), endDate: new Date(), timestamp: new Date(), active: true });
};
我已经把它发布得很好,但是当我从日程安排中获得所有约会并将它们放在视图上时,(可能很明显)显示的是 ObjectId 而不是名称,所以这是:
<table style="width:100%;">
<tr>
<td class="left" ng-class="'head'">Time</td>
<td ng-class="'head'">Attendees</td>
<td ng-class="'head'">Description</td>
<td class="right" ng-class="'head'">Location</td>
</tr>
<tr ng-repeat="meeting in meetings">
<td class="left" ng-class-even="'even'">{{ meeting.startDate | date:"h:mma" }} - {{ meeting.endDate | date:"h:mma" }}</td>
<td ng-class-even="'even'"><span ng-repeat="person in meeting.participants">{{ person }}<span ng-hide="$last">, </span></span></td>
<td ng-class-even="'even'">{{ meeting.description }}</td>
<td class="right" ng-class-even="'even'">{{ meeting.location }}</td>
</tr>
</table>
变成这样:(我发布了一张图片,但我没有足够的声望点,所以显示的所有图片都是“参与者”列下方的用户的 ObjectId,而不是名称)
而且我想知道您应该如何正确链接事物以便能够显示名称而不是那些 ObjectId。
对不起小说,希望我说得足够清楚。