【问题标题】:Unexpected token ')' Nodejs with Couchdb带有 Couchdb 的意外令牌“)”Nodejs
【发布时间】:2022-01-18 18:09:11
【问题描述】:

我正在使用 nodejs 14 和 couchdb 数据库 3.1.1 我正在尝试使用 nodejs 连接并显示 couchdb 数据库列表,但出现以下错误

   const NodeCouchDb = require('node-couchdb');`
 
 
    const couch = new NodeCouchDb();
 
    // node-couchdb instance with Memcached
    const MemcacheNode = require('node-couchdb-plugin-memcached');
    const couchWithMemcache = new NodeCouchDb({
      cache: new MemcacheNode
   });
 
    // node-couchdb instance talking to external service
     const couchExternal = new NodeCouchDb({
     host: 'couchdb.external.service',
     protocol: 'https',
     port: 5984
    });
 
    // not admin party
    const couchAuth = new NodeCouchDb({
        auth: {
           user: 'admin',
          pass: 'godhelp'
       }
    });

      couch.listDatabases()
        .then(
           dbs => dbs.map(...), 
            err => {
           // request error occured 
      });

编译后出错:

        dbs => dbs.map(...), 
                     ^

    SyntaxError: Unexpected token ')'
    at wrapSafe (internal/modules/cjs/loader.js:988:16)
    at Module._compile (internal/modules/cjs/loader.js:1036:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

【问题讨论】:

    标签: node.js database express nosql couchdb


    【解决方案1】:

    我认为您的代码中存在语法错误,但更根本的是node-couchdb 似乎是一个四年多没有更新的非活动库。我建议使用nano,这是官方的 Apache CouchDB NodeJS 库。

    这是一个使用 nano 列出您的数据库的简单 NodeJS 脚本 (index.js)。

    const nano = require('nano')(process.env.COUCH_URL)
    
    const main = async function() {
      const response = await nano.db.list()
      console.log(response)
    }
    
    main ()
    

    您需要创建一个名为 COUCH_URL 的环境变量:

    export COUCH_URL="http://user:password@localhost:5984"
    

    然后运行脚本:

    node index.js
    #[ 'deeds' ]
    

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 2014-10-24
      • 2016-12-12
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多