Javascript 如何将mongodb数据库的内容显示到前端?
基本上,我需要从html中检索用户输入。使用该输入通过mongoose搜索我的mongoDB数据库。然后检索该信息并将其显示到前端。我不知道该怎么做。这是一个更大的问题。让我帮你指出一些细节Javascript 如何将mongodb数据库的内容显示到前端?,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,基本上,我需要从html中检索用户输入。使用该输入通过mongoose搜索我的mongoDB数据库。然后检索该信息并将其显示到前端。我不知道该怎么做。这是一个更大的问题。让我帮你指出一些细节 使用mongoose连接器定义mongo模型 在nodejs代码中创建get路由以处理来自客户端的调用 使用fetch或任何客户端http包装器对节点服务器进行调用 相应的模型或服务写入模型。查找并返回响应 检索数据意味着向节点端点发送Get请求 由于我使用axios,我建议您也使用axios(如果您使用r
检索数据意味着向节点端点发送Get请求 由于我使用axios,我建议您也使用axios(如果您使用react或任何其他框架,请安装npm axios),或者您可以简单地复制它的脚本CDN 从前端,您只需
axios.get("Your url address" + "api route address").then((response) => {
//do whatever with your response
}).catch(error => {
//Do something in case of error
})
以实际地址为例,假设我有一个节点服务器连接到运行在本地主机8000
上的mongoose,其中我的api端点如下所示(后端)
我像这样导入了我的用户模式
const User = require("./../models/userSchema.js")
const User = require("./../models/userSchema.js")
router.get("/", async (req, res) => {
const contactList = await User.find({}) //coming from mongoose
res.send(contactList)
})
const mongoose = require('mongoose')
const userSchema = new mongoose.Schema({
firstName: String,
lastName: String,
address: String,
email: {
type: String,
default: "www.xyz@abc.com"
},
number: Number,
OTP: Number,
createdAt: {type: Date, default: Date.now},
DateOfBirth: {
type: String,
default: "1/01/2001"
},
image: {
type: String,
default: "http://icons.iconarchive.com/icons/graphicloads/100-flat/256/contact-icon.png"
},
})
module.exports = mongoose.model("User", userSchema)
通过axios,我的api请求如下(前端)
后端部分->发布请求
首先像这样定义并导出mongoose模式
const User = require("./../models/userSchema.js")
const User = require("./../models/userSchema.js")
router.get("/", async (req, res) => {
const contactList = await User.find({}) //coming from mongoose
res.send(contactList)
})
const mongoose = require('mongoose')
const userSchema = new mongoose.Schema({
firstName: String,
lastName: String,
address: String,
email: {
type: String,
default: "www.xyz@abc.com"
},
number: Number,
OTP: Number,
createdAt: {type: Date, default: Date.now},
DateOfBirth: {
type: String,
default: "1/01/2001"
},
image: {
type: String,
default: "http://icons.iconarchive.com/icons/graphicloads/100-flat/256/contact-icon.png"
},
})
module.exports = mongoose.model("User", userSchema)
然后,希望您已经使用MVC模式配置了节点,将其导入到需要使用模式的路由中
const express = require("express")
const router = express.Router()
const User = require("./../models/userSchema.js")
在该模式内创建一个接受post请求(或put请求)的API路由或端点
一旦您填写了您要获取的数据(req.body
包含数据),您就需要保存它,并按照上述路线进行扩展
router.post("/message", async (req, res) => {
const newMessage = new User({
firstName: req.body.(whatever from your request contains firstName
.....
......
)}
newMessage.save().then((response) => {
if (error) {
console.log(error)
throw new Error (error)
} else {
console.dir(responseData)
res.send(responseData)
}
})
})
前端部件
由于我使用axios,我建议您也使用axios(如果您使用react或任何其他框架,请安装npm axios),或者您可以简单地复制它的脚本CDN
在axios内部,我们发送post请求
axios.post("Your port address" + Message , object).then(response => {
console.log(response)
}).catch(error => {
console.log(error)
})
欢迎来到堆栈溢出。有很多这样的博客-。如果您有具体问题,请仔细阅读并返回stackoverflow。