express.js上的猫鼬人口问题

时间:2020-06-25 12:14:13

标签: express mongoose

您好evryone我最近两天发布了tryin来填充公司列表中的评论。

我正在尝试获取所有带有评论的公司列表。

每个评论都分配给公司ID。

关于邮递员的回复,我得到了一个空数组:[]

这是im所在的代码;

公司型号:

    var mongoose = require("mongoose");
    var Schema = mongoose.Schema;
    var slug = require("mongoose-slug-generator");
    
    mongoose.plugin(slug);
    
    var CompanySchema = new Schema({
        title: {type: String, required: true},
        slug: { type: String, slug: "title" },
        address: {type: String, required: true},
        totalRating: {type: Number, required: false},
        description: {type: String, required: false},
        facebookLink: {type: String, required: false},
        twitterLink: {type: String, required: false},
        googleLink: {type: String, required: false},
        linkedIn:{type: String, required: false},
        instagramLink:{type: String, required: false},
        contactNumber:{type: Number, required: false},
        website:{type: String, required: false},
        email:{type: String, required: false},
        review: [{type: Schema.Types.ObjectId, ref: "Review"}],
        user: { type: Schema.ObjectId, ref: "User", required: true },
    }, {timestamps: true});
    
    module.exports = mongoose.model("Company", CompanySchema);

评论模型:

            var mongoose = require("mongoose");
        
        var Schema = mongoose.Schema;
        
        var ReviewSchema = new Schema({
            title: {type: String, required: true},
            description: {type: String, required: true},
            rating: {type: String, required: true},
            user: { type: Schema.ObjectId, ref: "User", required: true },
            company: {type: Schema.ObjectId, ref:"Company"}
        }, {timestamps: true});
    
    module.exports = mongoose.model("Review", ReviewSchema);

我在哪里填充它:

    exports.companyList = [
        function (req, res) {
            try {
    
                Company.
                    find({id: req._id}).
                    populate("review").
                    then((companies)=>{
                        if(companies.length > 0){
                            return apiResponse.successResponseWithData(res, "Uspješno 1", companies);
                        }else{
                            return apiResponse.successResponseWithData(res, "Uspješno 2", []);
                        }
                    });
            } catch (err) {
                //Baci error 500...
                return apiResponse.ErrorResponse(res, err);
            }
        }
    ];

我在前面尝试过evrything。

1 个答案:

答案 0 :(得分:0)

尝试更改您的Company Model

review: [{type: Schema.Types.ObjectId, ref: "Review"}]

review: {type: Schema.Types.ObjectId, ref: "Review"}

然后,当您尝试填充时,我假设您是通过_id的公司paramsbody的{​​{1}}来通过公司的request,因此很可能是:< / p>

req.params.id

req.body.id

实际功能:

module.exports = {
  fncName: (req, res) => {
    Company.findById(req.params.id)
      .populate({
      path: 'review',
    })
      .exec((err, company) => {
      if (!err) {
        if(companies.length > 0){
          return apiResponse.successResponseWithData(res, "Uspješno 1", companies);
        }else{
          return apiResponse.successResponseWithData(res, "Uspješno 2", []);
        }
      }else {
        console.log(err)
      }
    })
  }
}

我不确定您如何注册路由器,但看起来可能像这样:

const router = require ('express-promise-router')();
router.get('/get-company', nameOfTheController.fncName);

注意:要填充来自Company Model的评论,您不一定需要在Review Model中添加以下行:

company: {type: Schema.ObjectId, ref:"Company"}