当我发布到一个路由时,我一直得到404。我正在使用MongoDB,Express和React Native。我已经创建了架构,操作和路由器。
架构如下所示:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Edible Schema
const EdiblesSchema = new Schema({
name: {
type: String,
required: true
},
price: {
type: String,
required: true
},
strength: {
type: String,
required: true
},
purchasedLocation: {
type: String,
required: true
},
effects: {
type: String,
required: true
},
strain: {
type: String,
required: true
},
});
module.exports = Edibles = mongoose.model("edibles", EdiblesSchema);
然后发送路由器如下所示:
const express = require("express");
const router = express.Router();
const keys = require("../../config/keys");
// Load edibles model
const Edibles = require("../../models/edibles");
// @route POST api/users/edibles
// @desc Add an edible to the users collection
// @access Public
router.post ('/edibles', (req, res) => {
if (res.status === 200) {
const newEdible = new Edibles({
name: req.body.name,
price: req.body.price,
strength: req.body.strength,
strain: req.body.strain,
purchasedLocation: req.body.purchasedLocation,
effects: req.body.effects,
})
} else {
return res.status(400).json({ email: "Thats not going to get you high" });
} newEdible
.save()
.then( edibles => res.json(edibles))
.catch(err => console.log(err))
});
module.exports = router;
最后,我们使用handleSubmit将用户信息发送到API端点。
const handleSubmit = (response) => {
console.log("EDIBLES", name, strength, price, effects, strain)
dispatch(setEdible(payload))
if (response === 200) {
axios
.post(edibleApi, payload)
console.log("PAYLOAD", edibleApi, payload, setEdible)
// navigation.navigate("Dashboard")
} else {
console.log("WRONG", edibleApi, payload, setEdible);
console.log("userEdibles", userEdibles)
}
}
我真的搞不懂...请帮帮我!
1条答案
按热度按时间tez616oj1#
问题是我使用了错误的端点,我通过使用express-list-routes将其添加到我的server.js文件.
const expressListRoutes = require('express-list-routes');
中解决了这个问题const路由器= express.路由器();
console.log(“现在我们用煤气做饭”,expressListRoutes(应用程序,{前缀:“/API/v1”})))
必须将该console.log添加到指示服务器是否正在运行的console.log中....