使用 Node.js 的 MongoDB 引用我无法填充
Posted
技术标签:
【中文标题】使用 Node.js 的 MongoDB 引用我无法填充【英文标题】:MongoDB references with Node.js I can not populate 【发布时间】:2017-05-05 21:29:18 【问题描述】:我想在屏幕上显示用户的位置信息。
例如:
name: "Andy" surname : "Carol" City : "Istanbul" Town : "Kadıkoy"
当我调用getuser
函数时,我想显示城市和城镇名称。
这是我的代码:
用户模式
// Model for the User
module.exports = (function userSchema()
var Mongoose = require('mongoose');
var Schema = Mongoose.Schema;
var userSchema = new Schema(
name:
type: String,
require: true
,
surname:
type: String,
require: true
,
tel:
type: String,
require: true
,
age:
type: String,
require: true
,
mevki_id:
type: String,
require: true
,
lok_id: [
type: Mongoose.Schema.Types.ObjectId,
ref: 'locations'
]
);
var collectionName = 'users';
var USERSCHEMA = Mongoose.Schema(userSchema);
var User = Mongoose.model(collectionName, USERSCHEMA);
return User;
)();
用户控制器
//This Controller deals with all functionalities of User
function userController()
var User = require('../models/UserSchema');
// Creating New User
this.createUser = function (req, res, next)
var name = req.body.name;
var surname = req.body.surname;
var tel = req.body.tel;
var age = req.body.age;
var mevki_id = req.body.mevki_id;
var lok_id = req.body.lok_id;
User.create(
name: name,
surname: surname,
tel: tel,
age: age,
mevki_id: mevki_id,
lok_id: lok_id
, function (err, result)
if (err)
console.log(err);
return res.send(
'error': err
);
else
return res.send(
'result': result,
'status': 'successfully saved'
);
);
;
//Populateeee
this.getUser = function (req, res, next)
User.find().populate('lok_id')
.exec(function (err, result)
if (err)
console.log(err);
return res.send(
'error': err
);
else
return res.send(
'USERS': result
);
);
;
return this;
;
module.exports = new UserController();
位置架构
//Schema for Location
module.exports = (function LocationSchema()
var Mongoose = require('mongoose');
var Schema = Mongoose.Schema;
var LocationSchema = new Schema(
userid:
type: Mongoose.Schema.Types.ObjectId,
ref: 'users'
,
il:
type: String,
require: true
,
ilce:
type: String,
require: true
);
var collectionName = 'locations';
var LocationSCHEMA = Mongoose.Schema(schema);
var Location = Mongoose.model(collectionName, LocationSCHEMA);
return Location;
)();
位置控制器
//This Controller deals with all functionalities of Location
function locationController()
var location = require('../models/LocationSchema');
// Creating New Location
this.createLocation = function (req, res, next)
var userid = req.params.userid;
var il = req.params.il;
var ilce = req.params.ilce;
location.create(
userid: userid,
il: il,
ilce: ilce
, function (err, result)
if (err)
console.log(err);
return res.send(
'error': err
);
else
return res.send(
'result': result,
'status': 'successfully saved'
);
);
;
// Fetching Details of Location
this.getLocation = function (req, res, next)
location.find(, function (err, result)
if (err)
console.log(err);
return res.send(
'error': err
);
else
console.log(result);
return res.send(
'location Details': result
);
);
;
return this;
;
module.exports = new locationController();
【问题讨论】:
位置架构 i.imgsafe.org/93f9ceb4fa.jpg LocationController i.imgsafe.org/93f9b72897.jpg 最好将代码包含为文本而不是位于外部站点的屏幕截图。 我编辑了你 【参考方案1】:我已经遇到了模型定义问题。 通过将第三个参数添加到 mongoose.model(显式集合名称)来修复它
// Try to replace :
var collectionName = 'users';
var USERSCHEMA=Mongoose.Schema(userSchema);
var User = Mongoose.model(collectionName, USERSCHEMA);
// with:
var collectionName = 'users';
var USERSCHEMA=Mongoose.Schema(userSchema);
var User = Mongoose.model(collectionName, USERSCHEMA, collectionName);
collectionName 必须在模式定义或模型定义中设置。更多详情见here
【讨论】:
你说的对,但我的创建用户函数是错误的,你可以在用户控制器中查看这个函数吗?以上是关于使用 Node.js 的 MongoDB 引用我无法填充的主要内容,如果未能解决你的问题,请参考以下文章
用Node.JS+MongoDB搭建个人博客(model目录)
用Node.JS+MongoDB搭建个人博客(万众期待的router.js)