我有一个接受route参数的端点。表单route接受route参数,并使用该参数使用Mongoose在Forms集合中找到相应的表单。但是,当我检索文档时,将返回“ fields”属性(它是一个数组),而数组中不包含任何元素。
表单集合的架构如下:
const FormSchema = new Schema({
name: String,
fields: [
{
name: String,
label: String,
type: String,
validation: { required: Boolean, min: Number, max: Number || null },
},
],
});
mongoose.model("forms", FormSchema);
我的端点看起来像这样:
app.get("/api/forms/:formName", (req, res) => {
const formName = req.params.formName;
Forms.find({name: formName }).then((form) => {
if (form) {
return res.send(form);
}
return res.send({ error: "No form found" });
});
});
当前端返回响应时,即使fields数组中有元素,“ fields”属性也只是一个空数组。
尝试这个:
架构图
const Forms = mongoose.model("forms", FormSchema);
使用自己的路径将此文件添加到端点文件中
const Forms = require('../models/forms');
也许您可以将其用作端点:
app.get('/api/forms/:formName', (req, res, next) => {
try {
const formName = req.params.formName;
const foundForm = await Forms.find({name: formName})
if (!foundForm) return next(new Error('No form found'));
res.status(200).json({
success: true,
data: foundForm
});
} catch (error) {
next(error);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句