将数据从React JS转移到MongoDB
我试图将数据从前端(React JS)发送到后端(节点JS),然后将数据发送到MongoDB数据库(因此将其保存在那里)。我用数据成功地调用了服务器,但是我无法将日期从服务器发送到数据库。这些是我的文件。
react js file :(当用户输入一些文本并单击按钮上时称此函数是调用的
handleSubmit = () => {
console.log("its running");
let databody = {
message: this.state.val,
};
console.log(" the message is :" + this.state.val);
return fetch("http://localhost:5000/stored", {
method: "POST",
body: databody,
headers: {
"Content-Type": "application/json",
},
})
.then((res) => res.json())
.then((data) => console.log(data));
};
) ”)
const express = require("express");
const cors = require("cors"); // Importing cors
var request = require("request");
const dotenv = require("dotenv");
const port = 5000;
var util = require("util");
const connectDB = require("./config/db");
require("dotenv").config({ path: "./config/config.env" });
const app = express();
dotenv.config();
const db = connectDB();
app.get("/", (req, res) => {
res.send("Hey there!");
});
app.get("/Pinged", function (req, res) {
res.send("Pinged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
});
app.use(cors({ origin: "*" }));
app.post("/stored", (req, res) => {
console.log("its running 2: " + req.body);
db.collection().insertOne(req.body, (err, data) => {
if (err) return console.log(err);
res.send("saved to db: " + data);
});
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
配置文件夹中的db.js文件:
const mongoose = require("mongoose");
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
console.log(`MongoDB Connected : ${conn.connection.host}`);
return conn;
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
module.exports = connectDB;
I'm trying to send data from the front-end(react js) to the back-end(node js) and then to mongodb database (so it would be saved there). I called the server successfully with the data, but I'm not able to send the date to the database from the server. These are my files.
react js file: ( this function is called when the user enters some text and clicks on a button )
handleSubmit = () => {
console.log("its running");
let databody = {
message: this.state.val,
};
console.log(" the message is :" + this.state.val);
return fetch("http://localhost:5000/stored", {
method: "POST",
body: databody,
headers: {
"Content-Type": "application/json",
},
})
.then((res) => res.json())
.then((data) => console.log(data));
};
index.js - nodejs file: (Here is where I'm getting my error which says "TypeError: connectDB.collection is not a function")
const express = require("express");
const cors = require("cors"); // Importing cors
var request = require("request");
const dotenv = require("dotenv");
const port = 5000;
var util = require("util");
const connectDB = require("./config/db");
require("dotenv").config({ path: "./config/config.env" });
const app = express();
dotenv.config();
const db = connectDB();
app.get("/", (req, res) => {
res.send("Hey there!");
});
app.get("/Pinged", function (req, res) {
res.send("Pinged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
});
app.use(cors({ origin: "*" }));
app.post("/stored", (req, res) => {
console.log("its running 2: " + req.body);
db.collection().insertOne(req.body, (err, data) => {
if (err) return console.log(err);
res.send("saved to db: " + data);
});
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
db.js file inside config folder:
const mongoose = require("mongoose");
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
console.log(`MongoDB Connected : ${conn.connection.host}`);
return conn;
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
module.exports = connectDB;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,在DB.JS中,您应该返回conn.connection
Here, in db.js you should return conn.connection