Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use nodejs then

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to use nodejs then". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Usage of nodejs then: 1. Install mysql module under nodejs;2. Introduce js through "require()" method;3. Pass "Mysql.updateMysql(Name,Path).then(...) "Then you can use it.

本文操作环境:windows7系统、nodejs10.16.2版、Dell G3电脑。

nodejs 中.then的使用

在nodejs中我们怎么使用.then来返回结果集呢

1、nodejs下安装mysql模块

npm install mysql

2、这里是我们Mysql.js的配置以及函数

//引入数据库var mysql=require('mysql');//实现127.0.0.1本地链接var DATABASE = "test";var table='test'var connection = mysql.createConnection({ host:'127.0.0.1', user:'root', password:'root', port:'3306', database: DATABASE});//修改数据function updateMysqlToken(Id,Name,Path) { console.info('select Name====' + Name); console.info('select Path====' + Path); var Name = Name; var Path = Path;//主要是这里对Promise的使用,只有Promise才能使用.then return new Promise(function (resolve,reject){ //定义我们的sql var updateSql = 'UPDATE ' + table +' set Name = ?, Path = ?, where Id = ? '; //这里对应sql的字段 var updateParams = [Name,Path,Id]; //nodejs的执行 connection.query(updateSql,updateParams, function(err, rows, fields ) { if (err){ reject(err) } // console.log( `The solution is: ${rows.length } ` ); console.info(` the getRowByPkOne sql ${updateSql}`) if( !rows || rows.length == 0 ) resolve(null) else resolve(rows[0]) }) })}

3、下面编写我们的.then的使用,res.send返回的是一个JSON格式的数据

//引入js,这里是对数据库的连接配置var Mysql = require('Mysql.js'); Mysql.updateMysql(Name,Path).then( //updateMysql修改这里返回的值是个object对象,所以我们需要对其进行JSON解析 function(message) { console.info('message===' + JSON.stringify(message)); } ) //将结果集返回给前端 res.send(result); }"如何使用nodejs then"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report