我似乎不能引用2模式填充之后
I cant seem to reference 2 schemas to populate after
这是我想引用的两个模式。我希望listingsScheme userId引用userScheme的_id。已经花了2天时间了,请帮忙。
var userScheme = new Schema({
email: { type: String },
password: { type: String },
fullName: { type: String },
coverPhoto: { type: String},
profilePhoto: { type: String},
fbUserID: { type: String},
profileExtraInfo: { type: Array},
listings: {type: Schema.Types.ObjectId, ref: "Listings"}
var listingsScheme = new Schema({
category: { type: String, index: true },
title: { type: String },
description: { type: String },
attributes: { type: Array },
nrViews: {type: Number },
photos: { type: Array },
location: { type: Array, index: '2d' },
created: { type: Date, index: true },
updated: { type: Date },
active: { type: Boolean, index: true } ,
userId : { type: Schema.Types.ObjectId, ref : 'User' } ,
fullName : { type: String },
profilePhoto : { type : String},
followers : {type : String},
除了语法错误,你的代码应该可以工作。
- 记住使用复数模型名称(你有'User'=>'users')。
- 不要忘记为你的模式建模。
- 不要忘记使用。populate('userId')方法。
- 不要忘记连接服务器(发生的频率比你想象的要高)
- 确保你真的有文件彼此连接。
测试代码:
var mongoose = require('mongoose');
// connect with database.
mongoose.connect('mongodb://localhost:27017/temp');
var userScheme = new mongoose.Schema({
email: { type: String },
password: { type: String },
fullName: { type: String },
coverPhoto: { type: String},
profilePhoto: { type: String},
fbUserID: { type: String},
profileExtraInfo: { type: Array},
listings: {type: mongoose.Schema.Types.ObjectId, ref: "Listings"}
});
var listingsScheme = new mongoose.Schema({
category: { type: String, index: true },
title: { type: String },
description: { type: String },
attributes: { type: Array },
nrViews: {type: Number },
photos: { type: Array },
location: { type: Array, index: '2d' },
created: { type: Date, index: true },
updated: { type: Date },
active: { type: Boolean, index: true } ,
userId : { type: mongoose.Schema.Types.ObjectId, ref : 'User' } ,
fullName : { type: String },
profilePhoto : { type : String},
followers : {type : String}
});
// don't forget to 'model' your scheme.
var users = mongoose.model('User', userScheme);
var listings = mongoose.model('listings', listingsScheme);
// create mock user.
var user = new users({
email:'test'
});
// create mock listing with the user_id attached to property userId.
var listing = new listings({
userId:user._id
});
// save the created user and listing.
user.save();
listing.save();
// find ONE document in the collection 'listings'.
// populate the userId.
// execute the function and let mongoose call your callback.
listings.findOne().populate('userId').exec(function(err, doc)
{
// ensure there are no errors.
if(!err && doc)
{
// print the doc we found.
// should give userdoc inside userId property.
console.log(doc);
}
});
// clear collections.
//users.remove({});
//listings.remove({});
可运行链接:http://runnable.com/U24GoC9x3L9alfPc/output
相关文章:
- 从数据库预填充模式(非引导)
- 在引导模式中,点击相应的datatables行中的按钮,填充数据
- Django设计模式-在加载时填充客户端JavaScript变量的方法
- Twitter引导模式-从iframe中获取信息并填充在主页上
- 设置一个下拉填充到它's的值和文本在AngularJS和asp.net webApi中的编辑模式
- 在模式窗口中填充文本区域
- 单击链接时,无法将记录填充到引导模式
- 在主干视图中填充选择列表数据的常见模式
- 无法在 jquery 模式窗口中填充下拉列表
- CSS动画填充模式-我做错了什么
- 使用SimpleModalJS创建一个模式来填充浏览器窗口
- 覆盖动画填充模式:在JavaScript/CSS中转发
- 用AJAX填充引导模式体
- 创建HTML5画布模式并用它们填充内容
- 使用 JavaScript 模式填充文档
- 希望在SVG元素上组合CSS填充颜色和SVG模式
- 填充引导模式与内容,只是当点击
- 谷歌地图不填充模式弹出
- 我似乎不能引用2模式填充之后
- 如何使SVG图像模式填充移动与对象