2017-11-13 11 views
-1

NodeJSとMongooseに問題があります。 DBへの接続は立っていますが、そこからデータを取得することはできません。/api/bucketsにも問題なく接続できます。ここに私のコードは次のとおりです。NodeJS Mongooseが正しく動作しない

app.js

var express = require('express'); 
var app = express(); 
var bodyParser = require('body-parser'); 
var mongoose = require('mongoose'); 

Bucket = require('./models/bucket'); 

// Connect to Mongoose 
mongoose.connect('mongodb://localhost/worldbucket', function (err) { 
    if (err) throw err; 
    console.log('Successfully connected'); 
}); 


app.get('/', function (req, res) { 
    res.send('Please use sth other'); 
}); 

app.get('/api/buckets', function (req, res) { 
    Bucket.getBuckets(function (err, buckets) { 
     console.log("funkt"); 
     if (err) { 
      throw err; 
     } 
     res.json(buckets); 
    }); 
}); 

app.listen(3000); 
console.log('Running on port 3000'); 

とbucket.js:

var mongoose = require('mongoose'); 

// Bucket Schema 
var bucketSchema = mongoose.Schema({ 
    id: mongoose.Schema.Types.ObjectId, 
    creator: String, 
    text: String, 
    fulfilment: String, 
    latitude: Number, 
    longtitude: Number 
}); 

var Bucket = mongoose.model('bucket', bucketSchema); 

module.exports = Bucket; 

// get Buckets 
module.exports.getBuckets = (callback, limit) => { 
    Bucket.find(callback).limit(limit); 
} 

私はあなたが私を助けることができると思います。事前ご使用マングースのバージョン

答えて

1

イムわからないで

おかげで、彼らのドキュメントから

http://mongoosejs.com/docs/queries.html

// With a JSON doc Person. find({ occupation: /host/ }). limit(10). sort({ occupation: -1 }). select({ name: 1, occupation: 1 }). exec(callback);

だからあなたのケースであるべき

Bucket.find({}).limit(limit).exec(callback); 

助けてください。

0

mongoでコレクションの名前を確認してください。bucketsではなく、bucketとしてください。それは複数である必要があります。あなたのコードが動作することは別として、私はそれをテストしました。 Why does mongoose always add an s to the end of my collection name

:別のSO、ここでこのトピックに関するスレッドがあり

> db 
worldbucket 
> db.buckets.insert({"creator":"me","text":"hello world"}) 
WriteResult({ "nInserted" : 1 }) 
> db.buckets.find() 
{ "_id" : ObjectId("5a0a154a29642fd7a970420e"), "creator" : "me", "text" : "hello world" } 


$ curl http://localhost:3000/api/buckets 
[{"_id":"5a0a154a29642fd7a970420e","creator":"me","text":"hello world"}] 

関連する問題