【发布时间】:2017-12-03 04:48:22
【问题描述】:
拜托,我在这里做错了什么?我的搜索/查询全部返回空。我尝试了两种方法(注释掉其中一种)。
app.get('/dashboard', isLoggedIn, function(req, res) {
//check if i still have logged in user in session
console.log(req.user._id); //confirmed, I have it
var owner = req.user._id; //assign user id to var owner
//create a query
var query = {createdby: owner};
//first method using where clause
Election.find().where('createdby').equals(owner)
.exec(function(err, elections) {
if (err) { return next(err); }
console.log(elections);
res.render("dashboard", { elections: elections });
});
//second method
/**Election.find(query)
.sort({ createdAt: "descending" })
.exec(function(err, elections) {
if (err) { return next(err); }
console.log(elections);
res.render("dashboard", { elections: elections });
});**/
//both methods returned empty
});
下面是模型,我真的看不出这里有任何错误。有人应该帮助我,我已经做了好几天了,谷歌没有帮助。
// models/election.js
// load the things we need
var mongoose = require('mongoose');
// define the schema for our election model
var electionSchema = mongoose.Schema({
local : {
title: String,
disc: String,
startdate: {type: String, required: true},
enddate: {type: String, required: true},
createdAt: {type: Date, default: Date.now()},
updatedAt: {type: Date, default: Date.now()},
createdby: {type: String},
ballots: {
ballot: { type: String }
}
}
});
// create the model for elections and expose it to our app
module.exports = mongoose.model('election', electionSchema);
【问题讨论】:
标签: node.js mongodb express mongoose