我目前有一个名为Consumer_Complaints的集合,位于一个名为consumer_Complaints的mongoDB数据库中(很抱歉让人感到困惑)。
我可以通过mongo shell访问Consumer_Complaints集合,但我无法在我的应用程序中访问它。我目前正在使用快递。
这是我的代码(更新):
var mongoose = require("mongoose");
//open connection to consumer_Complaints database
mongoose.connect('mongodb://localhost/consumer_Complaints')
//send error if connection fails
mongoose.connection.on('error', console.error.bind(console, 'connection error:' ));
var Schema = mongoose.Schema;
var complaintSchema = new Schema({
complaint_ID: String,
product: String,
sub_Product: String,
issue: String,
sub_Issue: String,
state: String,
zip_Code: String,
submitted_Via: String,
date_Received: String,
date_sent_to_Company: String,
company: String,
company_Response: String,
timely_Response: String,
consumer_Disputed: String,
});
complaintSchema.set('collection', 'Consumer_Complaints');
var model = mongoose.model("Model", complaintSchema)
model.find({}, function (err, results) {
res.json(results);
console.log(results);//Your Json result
});
Previous Answer- Found Here 这似乎是我想要的,但使用的语法与我在mongoose文档中看到的不同,当我尝试复制它时,我得到以下错误。
" TypeError:无法调用方法'找到'未定义"
任何帮助澄清链接帖子中的最佳答案,或弄清楚我做错了什么都将非常感谢!
答案 0 :(得分:0)
Model.find将返回一个回调。你可以尝试以下来替换你的console.log(Model.find())。
Model.find({}, function (err, docs) {
if (err) console.log(err);
console.log(docs);
});

此处提供更多文档:http://mongoosejs.com/docs/api.html#model_Model.find
答案 1 :(得分:0)
//定义的架构 var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var complaintSchema = new Schema({
complaint_ID: String,
product: String,
sub_Product: String,
issue: String,
sub_Issue: String,
state: String,
zip_Code: String,
submitted_Via: String,
date_Received: String,
date_sent_to_Company: String,
company: String,
company_Response: String,
timely_Response: String,
consumer_Disputed: String,
}
complaintSchema .set('collection','Consumer_Complaints');
然后在server.js中: -
app.get('/', function (req, res) {
console.log("I received a GET request")
complaintSchema.find({}, function (err, results) {
res.json(results);
console.log(results);//Your Json result
});
});