Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 了解如何在Node.js中将变量传递给Routes_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript 了解如何在Node.js中将变量传递给Routes

Javascript 了解如何在Node.js中将变量传递给Routes,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,您好,我有一个mongo函数的文件,其中我想将函数生成的值传递给routes文件,但我正在努力找到一种方法 labels.js const findLabels = function(db, callback) { // Get the documents collection const collection = db.collection(documentName); // Find some documents collection.find({}).toArray(fun

您好,我有一个mongo函数的文件,其中我想将函数生成的值传递给routes文件,但我正在努力找到一种方法

labels.js

const findLabels = function(db, callback) {
  // Get the documents collection
  const collection = db.collection(documentName);
  // Find some documents
  collection.find({}).toArray(function(err, docs) {
    assert.equal(err, null);
    console.log("Found the following records");
    console.log(docs)

    callback(docs);
  });
}

module.exports = {

  findLabels: () => MongoClient.connect(url, function (err, client) {
    assert.equal(null, err)
    console.log('Connected successfully to server')

    const db = client.db(dbName)

    findLabels(db, function () {

      client.close()
    })
  })
}
我想把文件传给routes

routes.js

router.get('/test', function (req, res, next) {
  db.findLabels()
  res.render('index')
})

更改导出版本的
findLabels()
的签名以接受回调函数。然后,您可以嵌套回调以创建一个控制流,其中
db.findLabels()
文档一直传递回路由

const findLabels = (db, cb) => {
  // Get the documents collection
  const collection = db.collection(documentName);

  // Find some documents
  collection.find({}).toArray((err, docs) => {
    // An error occurred we need to return that to the given 
    // callback function
    if (err) {
      return cb(err);
    }

    assert.equal(err, null);
    console.log("Found the following records");
    console.log(docs)

    return cb(null, docs);
  });
}

module.exports = {
  findLabels: cb => {
    MongoClient.connect(url, (err, client) => {
      if (err) {
        return cb(err)
      }

      console.log('Connected successfully to server')

      const db = client.db(dbName)

      findLabels(db, (err, docs) => {
        if (err) {
          return cb(err)
        }

        // return your documents back to the given callback
        return cb(null, docs)
      })
    })
  }
}
这是您的路由器,一旦返回标签,它就会将标签传递给您的
索引
路由

router.get('/test', (req, res, next) => {
  db.findLabels((err, labels) => {
    if (err) {
      return res.sendStatus(500)
    }

    res.render('index', {labels})
  })
})

... 使用您定义的回调。嘿,谢谢您的回答Kevin,我两天前刚刚启动Javascript,您能更具体一点吗?我如何使用回拨?谢谢你的帮助pete,我尝试了一下,得到了这个错误“TypeError:cb不是一个函数”指的是“返回cb(null,docs)”@Ricky你需要确保你使用的是我给出的路由器示例。谢谢你,我没有向你展示整个代码库,所以有些东西妨碍了你,但你的解决方案是有效的。