Javascript 即使MongoDB中的输入值不同,Express也会给出相同的响应
我是新的学习快车和MongoDB。我正在学习我的课程,我确信我的代码是完全相同的 我的问题: 当我将一些数据发布到MongoDB集合时,它会按预期工作。但当我尝试添加一个新值时,它是有效的,但插入的值与第一篇文章相同,即使输入值不同 以下是我的一些代码: PacientController.js routes/index.js: index.js: 注意:用户名和密码是正确的值,只是出于安全原因而受到谴责 我想知道为什么会这样。有什么缓存吗 测试 我在用邮递员做邮件测试。当我写第一篇文章时,我得到了这样一条信息:Javascript 即使MongoDB中的输入值不同,Express也会给出相同的响应,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我是新的学习快车和MongoDB。我正在学习我的课程,我确信我的代码是完全相同的 我的问题: 当我将一些数据发布到MongoDB集合时,它会按预期工作。但当我尝试添加一个新值时,它是有效的,但插入的值与第一篇文章相同,即使输入值不同 以下是我的一些代码: PacientController.js routes/index.js: index.js: 注意:用户名和密码是正确的值,只是出于安全原因而受到谴责 我想知道为什么会这样。有什么缓存吗 测试 我在用邮递员做邮件测试。当我写第一篇文章时,我得
El cliente se agregócorrectamente
,这意味着客户添加成功
但是,当我尝试向数据库添加一个新的寄存器时,我得到了相同的消息,但是当我更新数据库以查看新的更改时,我得到了新的寄存器,但是具有与第一篇文章相同的值
编辑
添加了
server.use(bodyParser.json())
但仍然得到相同的结果。您只导入routes文件夹,而不导入根文件index.js中的index.js
文件,以便导入为
const routes = require('./routes/index');
您正在JSON
对象中发送消息,但您没有;t在index.js
中使用中间件
,因此添加
server.use(bodyParser.JSON());
您的路由
和控制器
可以如下方式合并:在您的路由/index.js
文件中添加以下代码:
const express = require('express');
const router = express.Router();
const Paciente = require('../models/Paciente');
router.post('/pacientes', async (req, res) => {
const paciente = new Paciente(req.body);
try {
const saveData = await paciente.save();
res.json({ message: "El cliente se agregó correctamente" });
}
catch ( err ) {
res.json({ message: err });
}
});
//You can view all the data from here
router.get('/', async (req,res) => {
const data = await Paciente.find();
try {
res.json(data);
} catch( err ) {
res.json({ message: err })
}
});
module.exports = router;
现在,您可以删除根文件index.js
中的pacienteController.js文件,只导入routes文件夹,而不导入index.js
文件,以便导入
const routes = require('./routes/index');
您正在JSON
对象中发送消息,但您没有;t在index.js
中使用中间件
,因此添加
server.use(bodyParser.JSON());
您的路由
和控制器
可以如下方式合并:在您的路由/index.js
文件中添加以下代码:
const express = require('express');
const router = express.Router();
const Paciente = require('../models/Paciente');
router.post('/pacientes', async (req, res) => {
const paciente = new Paciente(req.body);
try {
const saveData = await paciente.save();
res.json({ message: "El cliente se agregó correctamente" });
}
catch ( err ) {
res.json({ message: err });
}
});
//You can view all the data from here
router.get('/', async (req,res) => {
const data = await Paciente.find();
try {
res.json(data);
} catch( err ) {
res.json({ message: err })
}
});
module.exports = router;
您现在可以删除pacienteController.js
文件我正确地执行了第一步,并在server.use之前添加了server.use(bodyParser.json())
行,但我得到了相同的结果。如果我尝试新代码,我会得到以下错误:/home/Alexisarieldominguezzanchez/Documentos/Programación/Curso de Javascript Moderno/API con Express Node MongoDB React Electron/API/Node_modules/Express/lib/router/index.js:139调试('dispatching%s',.method,req.url); ^ TypeError:无法读取Function.handle(/home/Alexisarieldominguezzanchez/Documentos/Programación/Curso de Javascript Moderno/API con Express Node MongoDB/API/Node_modules/Express/lib/router/index.js:139:34)中未定义的属性“方法”
您在github上有代码吗?您可以与我共享您的github链接抱歉,我在使用github时遇到问题。这是存储库:问题已解决将您的用户名
和密码
添加到数据库/keys.js
文件中,并在您的邮递员
中使用POST
方法保存数据库中的数据,使用URL作为http://localhost:3000/posts/pacientes
和在GET
方法中使用URL作为http://localhost:3000/posts
我正确地完成了第一步,并在server.use(bodyParser.json())
之前添加了server.use(bodyParser.urlencoded({extended:true}))
行,但我得到了相同的结果。如果我尝试新代码,我会得到以下错误:/home/Alexisarieldominguezzanchez/Documentos/Programación/Curso de Javascript Moderno/API con Express Node MongoDB React Electron/API/Node_modules/Express/lib/router/index.js:139调试('dispatching%s',.method,req.url); ^ TypeError:无法读取Function.handle(/home/Alexisarieldominguezzanchez/Documentos/Programación/Curso de Javascript Moderno/API con Express Node MongoDB/API/Node_modules/Express/lib/router/index.js:139:34)中未定义的属性“方法”
您在github上有代码吗?您可以与我共享您的github链接抱歉,我在使用github时遇到问题。这是存储库:问题已解决将您的用户名
和密码
添加到数据库/keys.js
文件中,并在您的邮递员
中使用POST
方法保存数据库中的数据,使用URL作为http://localhost:3000/posts/pacientes
和在GET
方法中使用URL作为http://localhost:3000/posts