[英]How to fix TypeError: registerUser is not a function
I am trying to register users and add them to a database, but when I call the registerUser function, an error gets thrown. 我试图注册用户并将其添加到数据库中,但是当我调用registerUser函数时,会引发错误。
I have read through this article to see how I would be causing this error and I have searched through several posts on here. 我通读了这篇文章,以了解将如何导致此错误,并且在这里搜索了几篇文章。
I feel like I must need another set of eyes to come and help find what I'm doing wrong 我觉得我需要另一只眼睛来帮助发现我在做错什么
Here is the code that calls the register user function ('data' is an object that is the cleaned inputs from the sign-up form) 这是调用注册用户功能的代码(“数据”是一个对象,是从注册表单中清除的输入)
const db = require('./database');
const bcrypt = require('bcrypt');
const saltRounds = 10;
function hasher(data){
bcrypt.hash(data.password, saltRounds)
.then(function(hash) {
data.password = hash;
db.registerUser(data);
})
.catch(e => console.error(e));
}
if I console.log(db) before registerUser is called heroku prints "{}" in the logs 如果在调用registerUser之前我console.log(db),则在日志中打印“ {}”
and this is the register user function from database.js: 这是来自database.js的注册用户功能:
const { Client } = require('pg');
const hasher = require('../models/hash')
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: true,
});
client.connect()
function registerUser(data) {
const query = "INSERT INTO users (user_id, first_name, last_name, email, username, hash_pass) VALUES(DEFAULT, $1, $2, $3, $4, $5)";
const values = [data.first, data.last, data.email, data.username, data.password];
client.query(query, values)
.then(res => console.log("stored: " + res.rows[0] + "in db"))
.catch(e => console.error(e.stack));
}
module.exports = {
registerUser : registerUser
};
I am getting "TypeError: db.registerUser is not a function" 我收到“ TypeError:db.registerUser不是函数”
The problem I guess is that you are importing const hasher = require('../models/hash')
into database.js
and const db = require('./database');
我猜的问题是,您正在将
const hasher = require('../models/hash')
导入database.js
和const db = require('./database');
into hasher. 进入哈希器。
That circular dependency may be causing the problem. 这种循环依赖关系可能会导致问题。
Your exports are fine. 您的出口很好。
From what I can see registerUser
is a standalone function, not a method of db
object. 从我可以看到,
registerUser
是一个独立的函数,而不是db
对象的方法。
I can also see module.exports = { registerUser : registerUser };
我还可以看到
module.exports = { registerUser : registerUser };
instruction - but this doesn't give a hint to where or what is it being exported to. 指令-但这并不能提示要导出到的位置或目标。
I suggest you try registerUser(data);
我建议您尝试
registerUser(data);
as a standalone declaration and see what happens, because obviously it is not becoming a method of db
- watch your console for any error messages during the load time... 作为一个独立的声明并查看会发生什么,因为显然它并没有成为
db
的方法-在加载期间监视控制台中是否有任何错误消息...
good luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.