集合未在节点js中定义

时间:2016-09-16 11:02:14

标签: node.js mongodb express

当我运行项目时,它说集合没有定义,但是当我从函数中取出集合时,它可以正常工作但在这种情况下我无法创建路径。有人请建议我帮助......

   var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/socialwiki", function(err, db) {
  if(!err) {
    console.log("We are connected");
 }});


exports.getprofile = function (req, res) {
   collection.find({}).toArray(function(err,res, docs) {
    if(err){
      res.send(err);
    }else if(res.length){
      console.log(res)
      }
      else{
        console.log('no docs found');
      }
    res.send(res)

  });
}

快递,

    var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var express = require('express')
  , cors = require('cors')
  , app = express();
var profile = require('../controller/user');
router.get('/getprofile', cors(), profile.getprofile);
module.exports = router;

1 个答案:

答案 0 :(得分:1)

  

确保最后导出路由器中的所有路由,如下所示:

 module.exports=router;

<强>更新

要获得收藏,您需要像上面这样定义:

var express = require('express');
var router = express.Router();
var collection=require('your collection');