I have seen some related questions at SO, but I can not find the correct solution to this problem. Below is a simplified version of my problem:
I have two simple models: user
and surf
.
The user
model contains sub-documents: board
. It has another property friend
which is an array of references to other users.
In the Surf
model, as the owner of user
surf, friend
as a subset of user's friends (i.e. Which of your friends is surfed with), and also is a reference to board_id
, which is part of the sub-document of the user described above. I am not registering bordesma
with Mongoose.
All the user's CRUDs operate and work well. When I pop the property of the surf shown in the controller, I am running a query to get all the surfs for a given user's friends, though it works, When I try to populate board_id
with name
and size
, I get a Mongoose error: schema The model "User.boards" has not been registered for
user
model: // requires user .model.js var mongoose = ('magos' ); Var Schema = Mongoose Sema; / ** * Board schema * / var boardschema = new schema ({name: string, size: string, category: string}); / ** * User Schema * / var UserSchema = New schema ({name: string, email: {type: string, lowercase: true}, board: [boardsize], friends: [{schema: type.object id, Referee: 'user'}]}); Module.exports = mongoose.model ('Users', Usascachema);
surf
model
// surf. Model.js var mongoose = is required ('navel'); Var Schema = Mongoose Sema; / ** * Surf schema * / var SarfSamma = new schema ({user_id: {type: Schema.Types.ObjectId, Reference: 'user'}, buddies: [{type: Schema.Types.ObjectId, ref: 'User' } Type: date_id: {type: schema} type object id, re: 'user board', required: true}, comment: {type: string, required: wrong}, session date: {type: date, required: True},}, {collection: 'surfs' // mongo equalizes 'surf' to 'servers', so clearly defined}); Module Exports = Mongos Model ('surf', sarfuska);
Surf
Controller
// surf.ccontroller.js var mongoose = Requires ('Negula'); Var requires user = ('../ user / user.mold') var sarf = is required ('./surf.model'); Export.feed = function (Rick, Race) {var userId = req.user._id; Var userIds = req.user.friends; Surf ({User_id: {$ in: userIds}}) Populate ('user_id', 'name email') // Populate ('board_id', 'name size'). Populate ('friends', 'name email') .exec (function (error, surfers) {if (err) {return handle alter (res, err);} return res.json (200, surfs);}); };
In the controller .populate ('board_id', 'name size')
has been commented which is the cause of the problem. I am not registering with BoardSchema
with Mongoos because it is not a sub-document for the model is not I should be? Or is it another way to write this question to populate the board
from the sub-document of the right user's board? Thanks for the help!
change
board: {type: schema Type: Object ID, Ref: 'User. Board ', Required: True},
with
Board: {type: Schema.Types. Object ID, Ref: 'Userboard', Required: True},
It should work.
Bourgesemma
is not for sub-documents. This is a new collection, such as User Collections. Whatever you have done, the board documents are referred to with the boards
property.
No comments:
Post a Comment