我在向 mongodb 发送 POST 请求时遇到问题。我的架构和请求是否正确?
I'm having trouble sending POST request to mongodb. Is my schema and request correct?
我正在尝试创建一个用于显示食物食谱的后端。一个食谱可以有很多成分,所以我为它们想出了一个模式。
这是食谱架构
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const RecipeSchema = new Schema({
title: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
ingredients: [
{ type: mongoose.Schema.Types.ObjectId, ref: 'ingredients' },
],
});
module.exports = mongoose.model('Recipe', RecipeSchema);
这是成分架构
const mongoose = require('mongoose');
const IngredientSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
imageUrl: {
type: String,
required: false,
},
});
module.exports = mongoose.model('Ingredients', IngredientSchema);
我已经在 mongodb 数据库中保存了一些成分。我可以通过发送 GET 请求来获取成分。
当我尝试 POST 数组中包含成分 ID 的食谱时遇到问题。我正在尝试 post 与 postman 并没有显示任何错误,但请求从未完成,它卡在发送请求上。
{
"title":"Some recipe",
"description":"Some recipe is very good",
"ingredients":["6106c09c19f8295424ea1969","6106c1137f104247d8c2d8ef"]
}
原来是 CORS 问题。使用 cors 为我修复了它。架构有效且 POST 正确
我正在尝试创建一个用于显示食物食谱的后端。一个食谱可以有很多成分,所以我为它们想出了一个模式。
这是食谱架构
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const RecipeSchema = new Schema({
title: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
ingredients: [
{ type: mongoose.Schema.Types.ObjectId, ref: 'ingredients' },
],
});
module.exports = mongoose.model('Recipe', RecipeSchema);
这是成分架构
const mongoose = require('mongoose');
const IngredientSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
imageUrl: {
type: String,
required: false,
},
});
module.exports = mongoose.model('Ingredients', IngredientSchema);
我已经在 mongodb 数据库中保存了一些成分。我可以通过发送 GET 请求来获取成分。 当我尝试 POST 数组中包含成分 ID 的食谱时遇到问题。我正在尝试 post 与 postman 并没有显示任何错误,但请求从未完成,它卡在发送请求上。
{
"title":"Some recipe",
"description":"Some recipe is very good",
"ingredients":["6106c09c19f8295424ea1969","6106c1137f104247d8c2d8ef"]
}
原来是 CORS 问题。使用 cors 为我修复了它。架构有效且 POST 正确