发送嵌套对象在GET请求中

huangapple go评论95阅读模式
英文:

Send nested object on GET

问题

以下是翻译好的部分:

我有一个非常基本的模式,其中包含另一个名为Vehicle的对象。

  1. let rentSchema = new Schema({
  2. code: {
  3. type: Number
  4. },
  5. vehicle: {
  6. type: mongoose.Schema.Types.ObjectId,
  7. ref: 'Vehicle'
  8. },
  9. ongoing: {
  10. type: Boolean,
  11. default: false
  12. }
  13. }, {collection: 'RentCollection'});

在控制器中查找所有:

  1. exports.getRent = function (req, res) {
  2. // 在数据库中查找
  3. rentSchema.find({}, function (err, rent) {
  4. if (err) res.status(400).send(err);
  5. res.json(rent);
  6. });
  7. };

响应作为Rents对象数组返回,但是Vehicle对象缺失在Rent对象中。这是为什么?

  1. _id: "5e04c19d0a0a100f58bd64b5"
  2. __v: 0
  3. ongoing: false
英文:

I have a very basic schema which has another object called Vehicle, inside

  1. let rentSchema = new Schema({
  2. code: {
  3. type: Number
  4. },
  5. vehicle: {
  6. type: mongoose.Schema.Types.ObjectId,
  7. ref: 'Vehicle'
  8. },
  9. ongoing: {
  10. type: Boolean,
  11. default: false
  12. }
  13. }, {collection: 'RentCollection'});

Find all in the controller

  1. exports.getRent = function (req, res) {
  2. // Find in the DB
  3. rentSchema.find({}, function (err, rent) {
  4. if (err) res.status(400).send(err);
  5. res.json(rent);
  6. });
  7. };

The response comes as an array of Rents but Vehicle object is missing from the Object Rent. Why is that?

  1. _id: "5e04c19d0a0a100f58bd64b5"
  2. __v: 0
  3. ongoing: false

答案1

得分: 1

以下是翻译好的部分:

1-) 首先,您需要创建一个模型并像这样导出它:

rent.js

  1. const mongoose = require("mongoose");
  2. const Schema = mongoose.Schema;
  3. let rentSchema = new Schema(
  4. {
  5. code: {
  6. type: Number
  7. },
  8. vehicle: {
  9. type: mongoose.Schema.Types.ObjectId,
  10. ref: "Vehicle"
  11. },
  12. ongoing: {
  13. type: Boolean,
  14. default: false
  15. }
  16. },
  17. { collection: "RentCollection" }
  18. );
  19. module.exports = mongoose.model("Rent", rentSchema);

2-) 假设您有这个车辆模型:

vehicle.js

  1. const mongoose = require("mongoose");
  2. const Schema = mongoose.Schema;
  3. let vehicleSchema = new Schema(
  4. {
  5. name: String
  6. },
  7. { collection: "VehicleCollection" }
  8. );
  9. module.exports = mongoose.model("Vehicle", vehicleSchema);

3-) 首先,让我们在VehicleCollection中创建一个车辆,像这样:

  1. {
  2. "_id": "5e0f465205515667746fd51a",
  3. "name": "Vehicle 1",
  4. "__v": 0
  5. }

4-) 然后,让我们使用这辆车的ID在RentCollection中创建一个租赁文档,如下所示:

  1. {
  2. "ongoing": false,
  3. "_id": "5e0f46b805515667746fd51d",
  4. "code": 1,
  5. "vehicle": "5e0f465205515667746fd51a",
  6. "__v": 0
  7. }

5-) 现在,我们可以使用以下代码来将车辆与租赁关联起来。

  1. const Rent = require("../models/rent"); //todo: change to path to the rent.js
  2. exports.getRent = function(req, res) {
  3. Rent.find({})
  4. .populate("vehicle")
  5. .exec(function(err, rent) {
  6. if (err) {
  7. res.status(500).send(err);
  8. } else {
  9. if (!rent) {
  10. res.status(404).send("No rent found");
  11. } else {
  12. res.json(rent);
  13. }
  14. }
  15. });
  16. };

6-) 结果将是:

  1. [
  2. {
  3. "ongoing": false,
  4. "_id": "5e0f46b805515667746fd51d",
  5. "code": 1,
  6. "vehicle": {
  7. "_id": "5e0f465205515667746fd51a",
  8. "name": "Vehicle 1",
  9. "__v": 0
  10. },
  11. "__v": 0
  12. }
  13. ]
英文:

Here is step by step explanations to make it work:

1-) First you need to create a model and export it like this:

rent.js

  1. const mongoose = require("mongoose");
  2. const Schema = mongoose.Schema;
  3. let rentSchema = new Schema(
  4. {
  5. code: {
  6. type: Number
  7. },
  8. vehicle: {
  9. type: mongoose.Schema.Types.ObjectId,
  10. ref: "Vehicle"
  11. },
  12. ongoing: {
  13. type: Boolean,
  14. default: false
  15. }
  16. },
  17. { collection: "RentCollection" }
  18. );
  19. module.exports = mongoose.model("Rent", rentSchema);

2-) Let's say you have this Vehicle model:

vehicle.js

  1. const mongoose = require("mongoose");
  2. const Schema = mongoose.Schema;
  3. let vehicleSchema = new Schema(
  4. {
  5. name: String
  6. },
  7. { collection: "VehicleCollection" }
  8. );
  9. module.exports = mongoose.model("Vehicle", vehicleSchema);

3-) First let's create a vehicle like this in the VehicleCollection like this:

  1. {
  2. "_id": "5e0f465205515667746fd51a",
  3. "name": "Vehicle 1",
  4. "__v": 0
  5. }

4-) Then let's create a rent document in RentCollection using this vehicle id like this:

  1. {
  2. "ongoing": false,
  3. "_id": "5e0f46b805515667746fd51d",
  4. "code": 1,
  5. "vehicle": "5e0f465205515667746fd51a",
  6. "__v": 0
  7. }

5-) Now we can use the following code, to populate the vehicle with the rents.

  1. const Rent = require("../models/rent"); //todo: change to path to the rent.js
  2. exports.getRent = function(req, res) {
  3. Rent.find({})
  4. .populate("vehicle")
  5. .exec(function(err, rent) {
  6. if (err) {
  7. res.status(500).send(err);
  8. } else {
  9. if (!rent) {
  10. res.status(404).send("No rent found");
  11. } else {
  12. res.json(rent);
  13. }
  14. }
  15. });
  16. };

6-) The result will be:

  1. [
  2. {
  3. "ongoing": false,
  4. "_id": "5e0f46b805515667746fd51d",
  5. "code": 1,
  6. "vehicle": {
  7. "_id": "5e0f465205515667746fd51a",
  8. "name": "Vehicle 1",
  9. "__v": 0
  10. },
  11. "__v": 0
  12. }
  13. ]

答案2

得分: 0

你需要使用 populate 方法来填充一个 vehicle 对象。

来自文档的示例:

  1. rentSchema.
  2. findOne({}).
  3. populate('vehicle').
  4. exec(function (err, obj) {
  5. if (err) return handleError(err);
  6. console.log(obj);
  7. });

另外,在你当前的代码中,你还没有设置模型:

RentCollection = mongoose.model('RentCollection', rentSchema);

英文:

You will have to use the populate method to populate a vehicle object.

From docs:

  1. rentSchema.
  2. findOne({}).
  3. populate('vehicle').
  4. exec(function (err, obj) {
  5. if (err) return handleError(err);
  6. console.log(obj);
  7. });

Also in your current code, you havent setted up model:

RentCollection = mongoose.model('RentCollection', rentSchema);

huangapple
  • 本文由 发表于 2020年1月3日 21:24:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定