Reputation: 37
first have user, then create store . how to do create store and ref to User?
// Schema
var UserSchema = new Schema({
name: String,
stores : [{ type: Schema.Types.ObjectId, ref: 'Store' }]
});
var StoreSchema = new Schema({
name: String,
user : [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
mongoose.model('Store', StoreSchema);
mongoose.model('User', UserSchema);
// express/mongoose save()
exports.storeAdd = function (req, res) {
new Store({
name : 'store 1',
user : [{_id:req.session._id}]
}).save();
};
It's not working, plz help thx
Upvotes: 1
Views: 218
Reputation: 946
var StoreSchema = new Schema({
name: String,
user : [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
This actually defines a one to many relationship between store and user as well. Is that intentional? If not you should remove the [, ] from around user, as well as in save().
Check to make sure that req.session._id has a valid mongoose Object ID, that is the mostly likely reason that the save is not working.
Upvotes: 1