猫鼬填充什么都不做
Mongoose Populate does nothing
我是 Node.js 的新手,尤其是 Mongoose。我正在通过教程学习。
我想我写了一个很好的简单代码来显示用户和他们的建筑物,但是 populate() 函数只显示对象的 _id :
我的模特:
(...) // omitted code
var userSchema = new Schema({
login: {
type: String,
required: 'Login is required',
unique: true,
index: true,
uniqueCaseInsensitive: true
},
(...) // omitted code
,
building: {
type: Schema.Types.ObjectId,
ref: 'Building'
}
createdAt: {
type: Date,
default: Date.now
}
});
var buildingSchema = new Schema({
address: {
type: addressSchema,
index: true,
unique: true
},
planPath: String,
createdAt: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('Building', buildingSchema);
module.exports = mongoose.model('User', userSchema);
我的路线:
app.route('/api/v1/users/:userId')
.get(parkingshare.readUser);
app.route('/api/v1/buildings/:buildingId')
.get(parkingshare.readBuilding);
我的控制器:
exports.readUser = function(req, res) {
user.findById(req.params.userId, function(err, user) {
if (err)
res.send(err);
res.json(user.populate('building'));
});
};
exports.readBuilding = function(req, res) {
building.findById(req.params.buildingId, function(err, building) {
if (err)
res.send(err);
res.json(building);
});
};
我用这些命令插入了数据:
curl --header "Content-Type: application/json" --request POST --data '{"address" : {"number":"01", "street":"test", "postCode":"01010", "city":"test"}, "planPath":""}' http://127.0.0.1:3001/api/v1/buildings
结果:
{
"_id": "6027c85fce48810533194d08",
"address": {
"number": 1,
"street": "test",
"postCode": "01010",
"city": "test"
},
"planPath": "",
"createdAt": "2021-02-13T12:38:55.677Z",
"__v": 0
}
curl --header "Content-Type: application/json" --request POST --data '{"login":"admin","password":"admin","lastName":"","firstName":"test","building": {"_id": "6027c85fce48810533194d08"},"parkLocations":[]}' http://127.0.0.1:3001/api/v1/users
结果:
{
"_id": "6027cab9ce48810533194d0f",
"login": "admin",
"password": "admin",
"lastName": "",
"firstName": "test",
"building": "6027c85fce48810533194d08",
"parkLocations": [],
"buildingAdmin": true,
"superAdmin": true,
"createdAt": "2021-02-13T12:42:40.908Z",
"__v": 0
}
现在,当我尝试通过浏览器填充 ('building') 的 readUser 方法获取用户时:
json result
为什么 populate() 在这里不起作用?我很困惑。
试一试:
exports.readUser = async function (req, res) {
try {
let result = await user.findById(req.params.userId).populate("building");
res.json({ result });
} catch (err) {
res.send(err);
}
};
或
exports.readUser = function(req, res) {
user.findById(req.params.userId).populate('building').exec(function (err, result) {
if (err) res.send(err);
res.json({ result });
})
}
我是 Node.js 的新手,尤其是 Mongoose。我正在通过教程学习。 我想我写了一个很好的简单代码来显示用户和他们的建筑物,但是 populate() 函数只显示对象的 _id :
我的模特:
(...) // omitted code
var userSchema = new Schema({
login: {
type: String,
required: 'Login is required',
unique: true,
index: true,
uniqueCaseInsensitive: true
},
(...) // omitted code
,
building: {
type: Schema.Types.ObjectId,
ref: 'Building'
}
createdAt: {
type: Date,
default: Date.now
}
});
var buildingSchema = new Schema({
address: {
type: addressSchema,
index: true,
unique: true
},
planPath: String,
createdAt: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('Building', buildingSchema);
module.exports = mongoose.model('User', userSchema);
我的路线:
app.route('/api/v1/users/:userId')
.get(parkingshare.readUser);
app.route('/api/v1/buildings/:buildingId')
.get(parkingshare.readBuilding);
我的控制器:
exports.readUser = function(req, res) {
user.findById(req.params.userId, function(err, user) {
if (err)
res.send(err);
res.json(user.populate('building'));
});
};
exports.readBuilding = function(req, res) {
building.findById(req.params.buildingId, function(err, building) {
if (err)
res.send(err);
res.json(building);
});
};
我用这些命令插入了数据:
curl --header "Content-Type: application/json" --request POST --data '{"address" : {"number":"01", "street":"test", "postCode":"01010", "city":"test"}, "planPath":""}' http://127.0.0.1:3001/api/v1/buildings
结果:
{
"_id": "6027c85fce48810533194d08",
"address": {
"number": 1,
"street": "test",
"postCode": "01010",
"city": "test"
},
"planPath": "",
"createdAt": "2021-02-13T12:38:55.677Z",
"__v": 0
}
curl --header "Content-Type: application/json" --request POST --data '{"login":"admin","password":"admin","lastName":"","firstName":"test","building": {"_id": "6027c85fce48810533194d08"},"parkLocations":[]}' http://127.0.0.1:3001/api/v1/users
结果:
{
"_id": "6027cab9ce48810533194d0f",
"login": "admin",
"password": "admin",
"lastName": "",
"firstName": "test",
"building": "6027c85fce48810533194d08",
"parkLocations": [],
"buildingAdmin": true,
"superAdmin": true,
"createdAt": "2021-02-13T12:42:40.908Z",
"__v": 0
}
现在,当我尝试通过浏览器填充 ('building') 的 readUser 方法获取用户时:
json result
为什么 populate() 在这里不起作用?我很困惑。
试一试:
exports.readUser = async function (req, res) {
try {
let result = await user.findById(req.params.userId).populate("building");
res.json({ result });
} catch (err) {
res.send(err);
}
};
或
exports.readUser = function(req, res) {
user.findById(req.params.userId).populate('building').exec(function (err, result) {
if (err) res.send(err);
res.json({ result });
})
}