在另一台计算机上运行的代码运行错误

时间:2019-08-15 21:24:30

标签: node.js mongodb

我不太确定发生了什么。运行'node ./controllers/db.controller.js'

时出现以下错误
1) (...).then(...).catch is not a function 

2) TypeError: resolve is not a function
PS P:\GitHub\arkadbot> node .\controllers\db.controller.js
(node:11944) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Connected successfully to server
{ user_id: 111 }
TypeError: findDocuments(...).then(...).catch is not a function
    at P:\GitHub\arkadbot\controllers\db.controller.js:73:19
    at new Promise (<anonymous>)
    at dbController (P:\GitHub\arkadbot\controllers\db.controller.js:35:11)
    at P:\GitHub\arkadbot\controllers\db.controller.js:132:11
    at P:\GitHub\arkadbot\node_modules\mongodb\lib\utils.js:410:17
    at executeCallback (P:\GitHub\arkadbot\node_modules\mongodb\lib\utils.js:402:9)
    at P:\GitHub\arkadbot\node_modules\mongodb\lib\operations\mongo_client_ops.js:286:5
    at connectCallback (P:\GitHub\arkadbot\node_modules\mongodb\lib\operations\mongo_client_ops.js:265:5)
    at P:\GitHub\arkadbot\node_modules\mongodb\lib\operations\mongo_client_ops.js:379:5
    at Server.connectHandler (P:\GitHub\arkadbot\node_modules\mongodb\lib\topologies\server.js:298:9)
find documents test 0
Found the following records
P:\GitHub\arkadbot\node_modules\mongodb\lib\utils.js:132
      throw err;
      ^

TypeError: resolve is not a function
    at P:\GitHub\arkadbot\db\bittrex\find_documents.js:29:11
    at P:\GitHub\arkadbot\node_modules\mongodb\lib\utils.js:410:17
    at executeCallback (P:\GitHub\arkadbot\node_modules\mongodb\lib\utils.js:402:9)
    at handleCallback (P:\GitHub\arkadbot\node_modules\mongodb\lib\utils.js:128:55)
    at P:\GitHub\arkadbot\node_modules\mongodb\lib\operations\cursor_ops.js:224:62
    at handleCallback (P:\GitHub\arkadbot\node_modules\mongodb\lib\utils.js:128:55)
    at completeClose (P:\GitHub\arkadbot\node_modules\mongodb\lib\cursor.js:898:14)
    at Cursor.close (P:\GitHub\arkadbot\node_modules\mongodb\lib\cursor.js:917:10)
    at P:\GitHub\arkadbot\node_modules\mongodb\lib\operations\cursor_ops.js:224:23
    at handleCallback (P:\GitHub\arkadbot\node_modules\mongodb-core\lib\cursor.js:204:5)
PS P:\GitHub\arkadbot>
C:\>node -v
v12.8.1

C:\>npm -v
6.10.2

如果我注释掉db.controller中的.catch并在findDocuments中将resolve更改为Promise.resolve,则文件运行良好。但是,如果数据库出现问题或其他问题,我们需要使用.catch。

该代码对于我编写该代码的好友来说很好用。我已经完成npm安装,卸载和重新安装nodejs,删除和重新安装节点模块以及四处移动代码。除此以外,我不完全确定该如何解决此问题。据我们所知,当我们运行“ node ls”时,我们拥有相同的程序包,那里也没有。

//===========================
// Import Libraries
//===========================

const assert = require('assert')
const Promise = require('promise')

//===========================
// Find Documents
//===========================
const findDocuments = function(db, collectionName, key, filterTerm) {         
  // Get the documents collection
  return new Promise ((resolve, reject) => {
    const collection = db.collection(collectionName)
    let filter = {};
    filter[key] = filterTerm;
    console.log(filter)
    switch (collectionName) {
      case "api_keys":
        collection.find(filter).toArray((error, docs)  => {
          if (error) reject (error)
          console.log('find documents test 0')
          assert.equal(error, null)
          console.log("Found the following records")
          resolve(docs)
        })
      break
      case "orders":
        collection.find({tradeId:field}).toArray((error, docs)  => {
          if (error) reject (error)
          assert.equal(error, null)
          console.log("Found the following records")
          resolve(docs)
        })
      break
      default: console.log('Insert Document Default Test')
    }
  })
}

module.exports = findDocuments
//===========================
// Database Controller
//===========================

const Promise = require('promise')

const mongoClient = require('mongodb').MongoClient
const assert = require('assert')

// Connection URL
const url = 'mongodb://localhost:27017'

// Database Name
const dbName = 'arkadbot'

// Create a new MongoClient
const client = new mongoClient(url)

// Database Services
const findDocuments = require('../db/find_documents.js')

// Database Collections
const order = require('../models/order.js')
const API = require('../models/api.js')

// Use connect method to connect to the Server
// Need to figure out how to pass { useNewUrlParser: true } to connect ***
const dbController = function dbController(serviceName, collectionName, exchange, userId, userAPIKey, userAPISecretKey, orderObject, db) {
    return new Promise((resolve, reject) => {
      collectionName = collectionName.toLowerCase()

    if (serviceName === 'find') {
        return findDocuments(db, collectionName, exchange, orderObject)
        .then(response => {
           console.log("Successfully found document from " + collectionName)
           resolve(response)
        })
        .catch(error => reject(error))
    } else  {
        console.log("There is an error with the service requested")
    }

    })

   }

module.exports = dbController


client.connect(function(error) {
   assert.equal(null, error)
   const db = client.db(dbName)

   return dbController('find','api_keys', 'user_id', 111, null, null, null, db)
   .then(response => {
      console.log("response", response)
      return dbController('find','api_keys', 'user_id', 123, db)
      .then(response2 => {
         console.log("response2", response2)
         client.close()
      })
      .catch(err => console.log(err))
   })
   .catch(err => console.log(err))
})

将promise语句更改为const Promise = require('Promise'),并在两个文件中更新了所有小写的Promise均为Promise。我现在有一个不同的错误,因为它的dbcontroller而不是查找文档

新错误:

PS P:\GitHub\arkadbot> node .\controllers\db.controller.js
(node:22940) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Connected successfully to server
{ user_id: 111 }
P:\GitHub\arkadbot\node_modules\mongodb\lib\topologies\server.js:301
          throw err;
          ^

TypeError: dbController(...).then(...).catch is not a function
    at P:\GitHub\arkadbot\controllers\db.controller.js:142:10
    at P:\GitHub\arkadbot\node_modules\mongodb\lib\utils.js:410:17
    at executeCallback (P:\GitHub\arkadbot\node_modules\mongodb\lib\utils.js:402:9)
    at P:\GitHub\arkadbot\node_modules\mongodb\lib\operations\mongo_client_ops.js:286:5
    at connectCallback (P:\GitHub\arkadbot\node_modules\mongodb\lib\operations\mongo_client_ops.js:265:5)
    at P:\GitHub\arkadbot\node_modules\mongodb\lib\operations\mongo_client_ops.js:379:5
    at Server.connectHandler (P:\GitHub\arkadbot\node_modules\mongodb\lib\topologies\server.js:298:9)
    at Object.onceWrapper (events.js:291:20)
    at Server.emit (events.js:203:13)
    at Pool.<anonymous> (P:\GitHub\arkadbot\node_modules\mongodb-core\lib\topologies\server.js:377:12)
PS P:\GitHub\arkadbot>

问题已解决!解决方案: 使用本机Promise并删除co​​nst promise / Promise = require('promise')语句可修复错误。

谢谢大家的帮助!

2 个答案:

答案 0 :(得分:1)

在第二个清单中,您导入promise,然后使用Promise

答案 1 :(得分:1)

如果您正在使用任何现代版本的node.js,则Promises是内置的。因此,无需这样做:

const promise = require('promise')

除非您专门尝试使用不同于node.js内置的Promise库。并且,如果您尝试这样做,则应始终使用它,并且某些代码使用{{1} },有些则使用promise来混合两者。

因此,我建议您摆脱Promise并在各处使用内置的const promise = require('promise')(请注意,您必须在代码中将Promise更改为promise正确地引用内置的Promise库。