初学者路上必不可少的荆棘
作为前端初学者而言,遇到了bug,搜索一天无果,想问问大佬们,能否帮我解决一下啊?
这是我的dbTools.js文件
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
const dbName = 'test';
var obj = {};
function __connect(callback) {
MongoClient.connect(url, function (err, client) {
if (err) throw err;
callback(client);
});
};
obj.insert = function (cname, arrData, fn) {
__connect(function (client) {
const col = client.db(dbName).collection(cname);
col.insert(arrData, function (err, result) {
fn(err, result);
client.close();
})
})
};
// obj.insert("test01",[{name:"jack"},{name:"rose"}],function (err,result) {
// if(err) throw err;
// console.log(result);
// })
// obj.find=function (cname, filter, fn) {
// __connect(function (client) {
// const col = client.db(dbName).collection(cname);
//
// col.find(filter).toArray(function (err, docs) {
//
// client.close();
// fn(err,docs)
// })
//
// })
// };
// obj.find("test01",{name:"jack"},function (err,users) {
// if(err) throw err;
// console.log(users);
// })
obj.update=function (cname, filter,update, fn) {
__connect(function (client) {
const col = client.db(dbName).collection(cname);
col.update(filter,{$set:update },function (err,result) {
client.close();
fn(err,result)
})
})
}
obj.delete=function (cname, filter, fn) {
__connect(function (client) {
const col = client.db(dbName).collection(cname);
col.deleteMany(filter, function (err, result) {
client.close();
fn(err, result)
})
})
}
// obj.update("test01",{name:"jack"},{name:"mick"},function (err,result) {
// if(err) throw err;
// console.log(result);
// })
// obj.delete("test01",{name:"mick"},function (err,result) {
// if(err) throw err;
// console.log(result);
// })
// export default obj;
我在以下的程序里调用dbTools.js里的find函数,
/**
* Created by LIF on 2019/4/15 0015.
*/
const express = require("express");
const fs = require("fs");
const formidable = require('formidable');
const path = require("path");
let server = express();
let router = express.Router();
const db = require('./dbTools');
let heros = [];
server.engine(".html", require("express-art-template"));
server.set("view engine", ".html");
router.get("/", (req, res, next)=> {
// res.render("index",{
// heros,
// });
find("heros", {}, (err, heros)=> {
res.render("index", {
heros
});
});
}).post("/add", (req, res, next)=> {
var form = new formidable.IncomingForm();
form.uploadDir = path.join(__dirname, "public", "imgs");
form.keepExtensions = true;
form.parse(req, function (err, fields, files) {
let nickname = fields.nickname;
let filename = path.parse(files.avater.path).base;
let img = "imgs/" + filename;
// heros.push({nickname,img});
insert("heros", {nickname, img}, function (err, result) {
alert("insert");
if (err) return next(err);
res.redirect("/");
});
// console.log(fields);
// console.log(files);
});
})
.all("*", (req, res)=> {
res.send("去首页看看")
});
// server.use(express.static("./js"));
server.use(express.static("./public"));
server.use(router);
// server.use((err,req,res,next)=>{
// console.log(err);
// res.send("<h1>亲爱的用户,您访问的页面有事,请去首页看看</h1>")
// });
server.listen(8888, ()=> {
console.log("8888")
});
网页报错:
请问,我是哪里错了呢?