简体   繁体   English

如何使用 nodejs 在 mysql 数据库中插入 npm uniqid 模块。 当我输入代码时,它显示错误“TypeError: uniqid is not a function”

[英]How to insert npm uniqid modules in mysql database using nodejs. when i put on my code it display an error "TypeError: uniqid is not a function"

I am using nodejs with express and mysql.我正在将 nodejs 与 express 和 mysql 一起使用。 when i submit a form i found an error "uniqid is not a function".当我提交表单时,我发现一个错误“uniqid 不是函数”。

var express = require('express');
var router = express.Router();
var uniqid = require('uniqid');
var db = require('../db');

router.post('/create',function(req,res,next){
    var full_name = req.body.fullname;
    var email = req.body.email;
    var password= req.body.password;
    var mobile_no = req.body.mobile_no;    
    var uniqid = uniqid();

    var insert_query = `INSERT INTO users (user_uid,full_name,email,mobile_no,password) VALUES ("${uniqid}","${full_name}","${email}","${mobile_no}","${password}") `;

    db.query(insert_query,function(err,result){
      if(err) throw err;
      res.redirect('/signup');
   });
});
module.exports = router;

You're overriding the uniqid module with the value from uniqid()您正在使用uniqid()的值覆盖uniqid模块

var uniqid = require('uniqid');
...
var uniqid = uniqid();

While it might work on the first execution 1 , subsequent calls will fail with that error "uniqid is not a function"虽然它可能适用于第一次执行1 ,但后续调用将失败并显示错误“uniqid is not a function”

Change the name and use es6 let/const instead of var更改名称并使用 es6 let/const而不是var

const uniqid = require('uniqid');
...
const newId = uniqid();
const insert_query = `INSERT INTO ... VALUES ("${newId}"...) `;

1 It doesn't, see Klaycon comment below 1不是,请参阅下面的 Klaycon 评论

Let's take this code for example :让我们以这段代码为例:

 var foo = () => "test" function test() { var foo = foo() } test()

As you can see, it has the same error you have.如您所见,它具有与您相同的错误。 This is because you are naming your local variable like your global variable.这是因为您将局部变量命名为全局变量。

What happens is what is called Hoisting .发生的事情就是所谓的提升 When the function gets executed, it declares foo as a variable local to the current function execution context before setting it any value.当函数被执行时,它设置任何值之前foo声明为当前函数执行上下文的局部变量。

Then the engine tries to set its value with foo() , but at that time foo is a local variable, not the global one, and its value is undefined .然后引擎尝试用foo()设置它的值,但当时foo是一个局部变量,而不是全局变量,它的值是undefined

And obviously, undefined is not a function, hence the error, "foo is not a function".显然, undefined不是函数,因此会出现错误“foo 不是函数”。

tl;dr: be careful when naming your variables (and use let / const ). tl;dr:命名变量时要小心(并使用let / const )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Function 在 nodejs 中未定义错误。 我的代码有什么问题? - Function is not defined error in nodejs. What's wrong with my code? 错误1452 MySQL和NodeJS。 数据库为什么不能正确引用我的表? - Error 1452 MySQL and NodeJS. How come the database cant reference my table right? 节点。 类型错误:https.request 不是函数 - NodeJS. TypeError: https.request is not a function 我正在使用Reactjs,nodejs开发应用程序。 我如何动态地连接两个不同的数据库(MySQL和Couchbase) - I am developing an application using Reactjs, nodejs. How can i connect two different databases(mysql and couchbase) dynamically 我无法通过nodejs在mongodb中插入数据。 请在下面的错误日志中查找 - I am not able insert data in mongodb by nodejs. Please find below the logs of the error 使用 nodejs 将表单数据插入 mysql。 我哪里错了? - Inserting form data into mysql using nodejs. Where am I going wrong? 如何从NodeJS执行Python。 从Python的函数中获取“无效语法” - How to execute Python from NodeJS. Getting “invalid syntax” in my function from python 如何在mysql数据库中插入我的代码JS - how to insert my code JS in mysql database Node.js。 module.export 有问题; 这不是函数错误 - NodeJs. Problem with module.export; it is not a function error 运行我的nodejs代码时收到此错误:'TypeError:无法读取未定义的属性'apply'。 ' - I get this error when running my nodejs code: ' TypeError: Cannot read property 'apply' of undefined. '
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM