如何解决"不是一个功能"在节点js中重新考虑db?

时间:2016-10-14 10:51:16

标签: node.js rethinkdb

TypeError: pollObject.addNewPolls is not a function
    at /home/imerit/iReNO/controllers/polls.js:23:16
    at Layer.handle [as handle_request] (/home/imerit/iReNO/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/imerit/iReNO/node_modules/express/lib/router/route.js:131:13)
    at next (/home/imerit/iReNO/node_modules/express/lib/router/route.js:125:14)
    at Route.dispatch (/home/imerit/iReNO/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/imerit/iReNO/node_modules/express/lib/router/layer.js:95:5)
    at /home/imerit/iReNO/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/home/imerit/iReNO/node_modules/express/lib/router/index.js:330:12)
    at next (/home/imerit/iReNO/node_modules/express/lib/router/index.js:271:10)
    at Function.handle (/home/imerit/iReNO/node_modules/express/lib/router/index.js:176:3)
    at router (/home/imerit/iReNO/node_modules/express/lib/router/index.js:46:12)
    at Layer.handle [as handle_request] (/home/imerit/iReNO/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/imerit/iReNO/node_modules/express/lib/router/index.js:312:13)
    at /home/imerit/iReNO/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/home/imerit/iReNO/node_modules/express/lib/router/index.js:330:12)
    at next (/home/imerit/iReNO/node_modules/express/lib/router/index.js:271:10)

这是我在尝试调用网址时遇到的错误。 polls.js代码如下。

"use strict";
var rethinkdb = require('rethinkdb');
var db = require('./db');
var async = require('async');

class polls {
  addNewPolls(pollData,callback) {
    async.waterfall([
      function(callback) {
        db.connectToDb(function(err,connection) {
          if(err) {
            return callback(true,"Error connecting to database");
          }
          callback(null,connection);
        });
      },
      function(connection,callback) {
        rethinkdb.table('poll').insert({
            "question" : pollData.question,
            "polls" : pollData.polls
        }).run(connection,function(err,result) {
          connection.close();
          if(err) {
            return callback(true,"Error happens while adding new polls");
          }
          callback(null,result);
        });
      }
    ],function(err,data) {
      callback(err === null ? false : true,data);
    });
  }
}

module.exports = polls;

我对节点js很新。有什么语法问题吗?我创建了控制器,模型和app.js。我的控制器包含home.jsindex.jspolls.js,模型包含db.jspolls.js。我想点击

http://localhost:3000/polls

我使用This Link作为参考

2 个答案:

答案 0 :(得分:0)

似乎你忘记了实现课程:

console.log(getDbConnection(query))

将module.exports替换为:

class A{ b(){console.log("sdsd");}}
A.b(); // undefined
(new A()).b(); // ok

答案 1 :(得分:0)

首先应该新建数据库对象:

var dbObject = new db();
  dbObject.connectToDb(function(err,connection) {
  // your code here.
}