【问题标题】:Why do I get this object when I query from MongoDB? [duplicate]为什么我从 MongoDB 查询时会得到这个对象? [复制]
【发布时间】:2017-10-15 15:53:49
【问题描述】:

伙计们,我不明白为什么我不从 MongoDB 获取数据。相反,我得到了这个对象:

   _mongooseOptions: {},
      mongooseCollection:
       NativeCollection {
         collection: null,
         opts: { bufferCommands: true, capped: false },
         name: 'products',
         collectionName: 'products',
         conn:
          NativeConnection {
            base: [Object],
            collections: [Object],
            models: [Object],
            config: [Object],
            replica: false,
..........
.....and so on

这是我的代码:

    var express = require('express');
    var router = express.Router();
    var mongoose = require('mongoose');
    var Product = require('../models/product');

        router.get('/', function(req, res, next) {
            var items = Product.find();
            console.log(items);

        });

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    Product.find() 返回一个查询对象。请参阅此处的文档:http://mongoosejs.com/docs/queries.html

    要获取实际数据,必须提供回调:

    router.get('/', function(req, res, next) {
      Product.find(function(err, items) { 
        console.log(items);
      });
    });
    

    您可以在官方快速入门中看到更多示例:http://mongoosejs.com/docs/index.html

    【讨论】:

    • 谢谢。它现在正在工作。
    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 2020-05-02
    相关资源
    最近更新 更多