如何从节点访问 mongoDB 集合数据

How to access mongoDB collection data from node

我正在尝试构建一个应用程序,它基本上会在 he/she 登录到 his/her 帐户后显示在我们数据库中注册的人的二维码。我能够将单个字段转换为二维码,但我想转换该特定用户的所有数据元素。需要帮助。

mongoose.connect(DB, {
                        useNewUrlParser: true,
                        useUnifiedTopology: true,
                        useCreateIndex: true,
                        useFindAndModify: false
}).then(()=>{
    console.log("connected to mongoDb Atlas");
}).catch((error)=>
        console.log("no connection"));

//Schema
var userSchema = {
    email: String,
    password: String,
    name: String,
}
var User = mongoose.model("User", userSchema);
const { check, validationResult} = require("express-validator");
app.use("/styles",express.static("styles"));
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.set("view engine","ejs");

//Navigation
app.get("/",function(req,res){
    res.render("index")
});
app.get("/login",function(req,res){
    res.render("login")
})
app.get("/register", function(req, res){
    res.render("register")
  })

app.post("/", function(req, res){
    bcrypt.hash(req.body.password, saltRounds, function(err, hash){
        let newUser = new User({ 
            email:req.body.email,
            password:hash, 
            name:req.body.name,
        });
    newUser.save();
    res.redirect("/");
    })
 
})

app.post("/login", function(req, res){
        const email = req.body.email;
        const password = req.body.password;
        const name = req.body.name;
        User.findOne({email:email}, function(err, foundUser){
                if (err){
                    res.send("Invalid login details");
                }else{
                        if (foundUser){
                        bcrypt.compare(password, foundUser.password, function(err, result){
                        if (result === true){
                           
                                
                            const url = req.body.email;
                            if (url.length === 0) res.send("Empty Data!");
                                qr.toDataURL(url,(err, src) => {
                                    if (err) res.send("Error occured");
                                    res.render("scan", { src });
                                });
                        }else{
                            res.send("Invalid login details! Try again.");
                         }
                        });
                        } 
                    }    
            }); 
})
app.listen(3000, function(){
    console.log("Server started at port 3000");
}) 

使用上面的代码,我只能获得一个字段的二维码,即 req.body.email 或 req.body.name,但我想在我的 MongoDB 中注册这两个字段的二维码数据库以及唯一 ID。

scan.ejs 文件

<%- include("./partials/header") %>
<body>
    <div class="container">
        <br>
        <h1 class="text-center">Scan Me!</h1>
        <hr>
        <div class="row mb-3">

        <div class="card" style="width: 18rem;">
            <img class="card-img-top" src=<%=src%> alt="QR Code Image">
            <div class="card-body">
            <p class="card-text">Scan the QR Code to access data!</p>
            </div>
            </div>
        </div>  
</body>
<%- include("./partials/footer") %>

答案在这里。

const url = foundUser.toString();
    if (url.length === 0) res.send("Empty Data!");
       qr.toDataURL(url,(err, src) => {
       if (err) res.send("Error occurred");
         res.render("scan", { src });
 });