1 回答
TA贡献1851条经验 获得超3个赞
从“模型产品”导出模型并在“产品控制器”中需要它。像这样:
产品型号:
const mongoose = require('mongoose');
const ProductSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
url: {
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now()
}
});
module.exports=mongoose.model('Product', ProductSchema);
产品控制器
const mongoose = require('mongoose');
const Product = require('path to model product.ex-../schemas/modelProduct')
module.exports = {
async index(req, res) {
const products = await Product.find();
return res.json(products);
}
}
添加回答
举报