我試圖從mongoDB中使用mongoose(最新版本)返回一個JSON對象。一切工作發現,但我得到一個空陣列返回[]。Mongoose返回empy對象,在mongo shell中工作
app.js
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////
dependencies
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
var express = require("express")
var cons = require("consolidate")
var app = express()
var db = require("./model/db")
var path = require("path")
var routes = require("./routes/routes")
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////
configure
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
app.configure(function(){
app.use(app.router)
app.engine("html", cons.hogan)
app.set("view engine", "html")
app.set("views", __dirname + "/views")
app.use(express.static(path.join(__dirname, "public")))
app.use(express.errorHandler())
})
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////
routes
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
app.get("/", routes.index)
app.get("/hire", routes.hire)
app.get("/hire/:id/:nr", routes.hirePerson)
app.get("/books", routes.books)
app.get("/projects", routes.projects)
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////
listen
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
app.listen(2020)
db.js
var mongoose = require('mongoose');
var teamSchema = new mongoose.Schema({
country: String,
GroupName: String
});
mongoose.model('Team', teamSchema);
mongoose.connect('mongodb://localhost/basingCOM');
var Team = mongoose.model('Team');
Team.find({"GroupName":"kevin"}, function (err, teams) {console.log(teams)})
這在外殼工作.find方法,我得到的JSON對象返回,但是當我」 m在我的節點應用程序中使用它,控制檯吐出一個空的對象。
20 Feb 11:47:06 - [nodemon] restarting due to changes...
20 Feb 11:47:06 - [nodemon] starting `node app.js`
[]
thx對於你所有的幫助傢伙,真的很感激。我越來越有緩慢而穩步地THX :)
編輯1
我改變db.js這(使用下面的評論),但我仍然有一個空的對象。
var mongoose = require('mongoose');
var teamSchema = new mongoose.Schema({
country: String,
GroupName: String
});
var teamModel = mongoose.model('Team', teamSchema);
mongoose.connect('mongodb://localhost/basingCOM', function(err){doDBstuff(err)});
function doDBstuff(err){
if (err){throw err}
console.log("jow")
teamModel.find({"GroupName":"kevin"}, function (err, teams) {console.log(teams)});
}
編輯2
我創建了一個新的模式(booksSchema)和試圖與booksModel的代碼,而不是teamModel。輸出是一個正確的JSON對象。所以它適用於booksModel,但不適用於teamModel。
var mongoose = require('mongoose');
mongoose.set('debug', true)
var teamSchema = new mongoose.Schema({
country: String,
GroupName: String
});
var booksSchema = new mongoose.Schema({
title: String,
author: String
});
var teamModel = mongoose.model('Team', teamSchema);
var booksModel = mongoose.model('books', booksSchema);
mongoose.connect('mongodb://localhost/basingCOM', function(err){doDBstuff(err)});
function doDBstuff(err){
if (err){throw err}
booksModel.find(function (err, books) {console.log(books)});
}
一些控制檯輸出:
> db.Team.find()
{ "_id" : ObjectId("5305d71aa753d02674ed311c"), "country" : "belgium", "GroupName" : "kevin" }
{ "_id" : ObjectId("5305d738a753d02674ed311d"), "country" : "holland", "GroupName" : "dave" }
>
> db.Team.find({"GroupName":"kevin"}, function (err, teams) {console.log(teams)})
{ "_id" : ObjectId("5305d71aa753d02674ed311c"), "country" : "belgium", "GroupName" : "kevin" }
>
EDIT 3
於星期一轉彎時goose.set(「debug」,true)表明mongo正在尋找集合teams.find()中的數據而不是Team.find()。
20 Feb 13:46:29 - [nodemon] starting `node app.js`
Mongoose: teams.find({}) { fields: undefined }
[]
我也試過你的方法,使用連接回調,但我仍然有一個空對象[] – kevinius
我剛剛從你的編輯1運行確切的代碼,我去在控制檯中輸入一個非空的數組。所以代碼很好。請分享你的mongo shell輸出,在那裏你得到正確的結果 – nabeel
nabeel,thx ...我改變了我的帖子,請閱讀我的編輯2和'從控制檯輸出' – kevinius