我正在创建一个基于用户的节点应用程序,并在其中实现DHTMLX Scheduler。我让调度程序工作并显示事件,唯一的问题是每个用户现在都可以看到和编辑相同的日历。
我尝试用引用创建模式,但是似乎没有用。
player.js模型(每个用户模式):
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
calendar: {
type: Schema.Types.ObjectId, ref: 'calendar'
}
});
const User = mongoose.model('player', UserSchema);
module.exports = User;
calendar.js模型:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const schema = new Schema({
text: {type: String, required = true},
start_date: {type: Date, required = true},
end_date: {type: Date, required = true},
user: {type: Schema.Types.ObjectId, ref = 'User', required = true}
});
const calendar = mongoose.model('calendar', schema);
module.exports = calendar;
实现我的app.js的日历部分
var db = require('mongoskin').db("myMongoDBCluster", { w: 0});
db.bind('calendar');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/init', function(req, res){
db.calendar.insert({
text:"My test event A",
start_date: new Date(2018,8,1),
end_date: new Date(2018,8,5)
});
db.calendar.insert({
text:"My test event B",
start_date: new Date(2018,8,19),
end_date: new Date(2018,8,24)
});
db.calendar.insert({
text:"Morning event",
start_date: new Date(2018,8,4,4,0),
end_date: new Date(2018,8,4,14,0)
});
db.calendar.insert({
text:"One more test event",
start_date: new Date(2018,8,3),
end_date: new Date(2018,8,8),
color: "#DD8616"
});
res.send("Test events were added to the database")
});
app.get('/data', function(req, res){
db.calendar.find().toArray(function(err, data){
//set id property for all records
console.log(err);
for (var i = 0; i < data.length; i++)
data[i].id = data[i]._id;
//output response
res.send(data);
});
});
app.post('/data', function(req, res){
var data = req.body;
var mode = data["!nativeeditor_status"];
var sid = data.id;
var tid = sid;
delete data.id;
delete data.gr_id;
delete data["!nativeeditor_status"];
function update_response(err, result){
if (err)
mode = "error";
else if (mode == "inserted")
tid = data._id;
res.setHeader("Content-Type","application/json");
res.send({action: mode, sid: sid, tid: tid});
}
if (mode == "updated")
db.calendar.updateById( sid, data, update_response);
else if (mode == "inserted")
db.calendar.insert(data, update_response);
else if (mode == "deleted")
db.calendar.removeById( sid, update_response);
else
res.send("Not supported operation");
});
答案 0 :(得分:0)
Mongo是非关系数据库,“引用”是mongoose提供的功能。要使用它,您必须查询猫鼬模型(从player.js和calendar.js导出的对象)
var Calendar = require('./calendar');
Calendar.find()
.then(function (data) {
// ...
});
Calendar.find({user: 'yourUserId'}) // query by specific user
.then(function (data) {
// ...
});