Node.js 无法使Nodejs express mongoose正常工作

Node.js 无法使Nodejs express mongoose正常工作,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,当我发布localhost:8000/api/customers时,即使我在mongodb中插入了数据,我也得到了空数组。我在没有连接到mongodb的情况下测试了localhost:8000/api/customers路由,它显示一个静态文本。但是,当连接到mongodb时,它返回空数组。这是密码 var express = require('express'), mongoose = require('mongoose'), bodyParser = require('body-parser

当我发布localhost:8000/api/customers时,即使我在mongodb中插入了数据,我也得到了空数组。我在没有连接到mongodb的情况下测试了localhost:8000/api/customers路由,它显示一个静态文本。但是,当连接到mongodb时,它返回空数组。这是密码

var express = require('express'),
mongoose = require('mongoose'),
bodyParser = require('body-parser');
mongoose.connect('mongodb://localhost/test');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection err'));
db.once('open', function callback(){console.log('connected to db')});
var Customer = require('./Customers/models/customerModel');
var app = express(),
port = process.env.PORT || 3000,
customerRouter = express.Router();

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
customerRouter.route('/Customers')
  .post(function(req, res){
    var customer = new Customer(req.body);
    console.log(customer);
    res.send(customer);
  })
  .get(function(req,res){
    Customer.find(function(err,customers){
        if(err)
            res.status(500).send(err);
        else {
            res.json(customers);
            console.log(customers);
        }
    });
  });
app.use('/api', customerRouter);
app.get('/', function (req, res) {
    res.send('Welcome to my API')
});
app.listen(port, function () {
    console.log('Running on port ' + port);
});
以及customerModel.js

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var customerModel = new Schema({
    name: {type: String},
    contact: {type: String},
    date_register: {type: String},
    main_tel: {type: String},
    tel_1: {type: String},
    tel_2: {type: String},
    tel_3: {type: String},
    address: {type: String},
    email: {type: String}
});

module.exports = mongoose.model('Customer', customerModel);

我正在端口8000上使用gulp task runner

您没有使用客户。请正确查找。查看文档:

find()
应该有两个参数。第一个参数是find方法的条件(“WHERE”条件,如果愿意的话),第二个参数是回调。从以下位置更新您的代码:

Customer.find(function(err,customers){ ...
为此:

Customer.find({}, function(err,customers){ ...
传递一个空的物体意味着“给我一切”。如果您以后想找到所有名为“John”的客户,您可以这样做:

Customer.find({firstName: "John"}, function(err,customers){ ...

好的,我解决了,我错过了客户。在帖子中保存(cb(err))。现在开始工作。谢谢大家

嗨,Ryan刚刚尝试过,没有结果,相同的东西你看到连接到db的
了吗
?Aleix是的,我看到了。在Ubuntu14.04上进行新的安装就可以发布代码了。get(function(req,res){Customer.find({},function(err,customers){if(err)res.status(500).send(err);else{res.json(customers);console.log(customers);}});调用---still Empty这是我在mongodb>db.Customer.find()中调用db集合的方式,我获取记录,但不是通过apiTry连接到默认端口上的db:
mongodb://localhost:27017/test
no luck这是gulp的日志-在连接到db[]的端口8000上运行如果你有一个与上面类似的工作示例,我会在我的ubuntu 16.04上尝试它