如何将数据库查找的数据显示在网页上(nodejs+mongodb) 30
index.js<!DOCTYPEhtml><html><head><title>reidea</title><linkrel='stylesheet'href='/st...
index.js
<!DOCTYPE html>
<html>
<head>
<title>reidea</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<form action="success" method="post">
<p>
<span >问题详情</span>
</p>
<!-- <div id="dbdata"></div> -->
</form>
</body>
</html>
/* reidea */
router.get('/reidea', function(req, res) {
db.open(function(err, db){
if(!err){
db.createCollection('mydb', {safe:true}, function(err, collection){
if(err){
console.log(err);
}else{
collection.find().toArray(function(err,docs){
console.log(docs);
//将数据显示到网页上
// console.log('1'+docs[0].name);
// $('#question').append('<div>'+docs+'</div>');
// document.getElementById("editLevels").value =docs;
});
}
});
}else{
console.log(err);
}
});
res.render('reidea', { title: 'reidea' });
}); 展开
<!DOCTYPE html>
<html>
<head>
<title>reidea</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<form action="success" method="post">
<p>
<span >问题详情</span>
</p>
<!-- <div id="dbdata"></div> -->
</form>
</body>
</html>
/* reidea */
router.get('/reidea', function(req, res) {
db.open(function(err, db){
if(!err){
db.createCollection('mydb', {safe:true}, function(err, collection){
if(err){
console.log(err);
}else{
collection.find().toArray(function(err,docs){
console.log(docs);
//将数据显示到网页上
// console.log('1'+docs[0].name);
// $('#question').append('<div>'+docs+'</div>');
// document.getElementById("editLevels").value =docs;
});
}
});
}else{
console.log(err);
}
});
res.render('reidea', { title: 'reidea' });
}); 展开
1个回答
2016-06-19
展开全部
nodejs操作mongodb查询所有数据的方法:
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';
var selectData = function(db, callback) {
//连接到表
var collection = db.collection('tb2');
//查询数据
var whereStr = {"name":'wilson001'};
collection.find(whereStr).toArray(function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
callback(result);
});
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
console.log("连接成功!");
selectData(db, function(result) {
console.log(result);
db.close();
});
});
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';
var selectData = function(db, callback) {
//连接到表
var collection = db.collection('tb2');
//查询数据
var whereStr = {"name":'wilson001'};
collection.find(whereStr).toArray(function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
callback(result);
});
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
console.log("连接成功!");
selectData(db, function(result) {
console.log(result);
db.close();
});
});
追问
我的数据已经拿到了 console.log(docs);,我想问如何把数据展示到网页上,也就是后台数据传递到前台
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询