Tag: callback

Node.js的mysqlcallback错误

我用node.js和mysql。 我在mysql查询callback中做了callback函数。 exports.callback = function(req) { …. Mysqldb.pool.getConnection(function(err, db) { db.query("select * from tbl"), function (err,rows) { db.query("update tbl set id = 1"), function (err, rows){ }); //update tbl });//select tbl }); //getconnection }; //export.callback 错误是更新查询callback….错误消息是 TypeError: this._callback.apply is not a function at Query.Sequence.end (c:\redfox_server_src\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24) at Query.ErrorPacket (c:\redfox_server_src\node_modules\mysql\lib\protocol\sequences\Query.js:94:8) at Protocol._parsePacket (c:\redfox_server_src\node_modules\mysql\lib\protocol\Protocol.js:274:23) at Parser.write (c:\redfox_server_src\node_modules\mysql\lib\protocol\Parser.js:77:12) at […]

node.js中的callback函数的作用域

我有以下代码: exported.removeAllWatchesForUser = function (projectKey, userObj, done) { console.log('done='+done); // Function is recognized // Get list of all issues & remove the user from watchers list exported.getIssueList(projectKey, function(err, response, done){ console.log('done='+done); // callback is not recognize but is 'undefined }); }; 我的问题是callback函数'done'在第2行中被识别,但在getIssueListcallback中的第6行中是'undefined'。 我如何使它在这个函数中可用,以便我可以传递callback到连续的asynchronous方法?

无法理解Connect模块中的callback

我在读一本关于NodeJs Connect的书。 有关于basicAuth模块的这个小部分。 我知道basicAuth已经被弃用了,但是我不能理解这个简单的代码。 这本书说 提供asynchronouscallback函数 最后一个选项是类似的,除了这次callback传递给了带有三个参数的basicAuth(),这个参数可以使用asynchronous查找。 从磁盘上的文件进行身份validation或从数据库查询时,这非常有用。 代码清单7.7 连接basicAuth中间件组件进行asynchronous查找 而没有其他的信息。 这就是关于在basicAuth中进行callback的整个部分 所以,代码获取用户名和密码。 然后假设的对象User有一个方法来authendicate这个用户是否真的存在。 完成后,调用gotUser函数。 gotUser包含返回的错误(=找不到用户名/密码的用户)或返回的用户对象(用该用户名/密码find的用户)。 我对吗? gotUser检查是否有错误。 如果有,则返回并调用具有错误参数的callback 。 那么等一下,这个callback做什么呢? 它没有在任何地方定义 它将错误传递给error handling函数吗? 如何? 如果没有错误, gotUser再次用null (=没有错误)和user gotUser调用callback 。 再一次,callback会做什么? 为什么将返回的用户传递给callback,而不是获取其名称,邮件,年龄等,并在会话中使用它们,或填充标记的innerHTML或其他? 谢谢

Nodejs如何在callback中访问全局val?

我在全局中定义了一个val,我希望在函数的callback中访问这个var,代码如下所示: exports.ready_to_exit = false; function send2kafkaImpl(payloads, cb) { kproducer.send(payloads, function(err) { if (!err) { if (ready_to_exit) { console.log('ready to exit'); process.exit(); } else { return cb(); } } console.log(new Date() + ' Kafka error: ', err.message || err); send2kafkaImpl(payloads, cb); }); } 当我运行这个代码时,出现错误: if (ready_to_exit) { ^ ReferenceError: ready_to_quit is not defined

有关承诺的细节; 例子

我努力实现诺言。 (我想我理解他们,但我经常得到意想不到的结果,所以也许我不会。) 请考虑这个代码。 function ap() { return new Promise(function(resolve, reject) { console.log('function ap starting want this'); function ender() { console.log('function ender starting want this'); return resolve('ender'); console.log('after return in ender don\'t want this'); //#1 } ender() console.log('after function ender and its resolve ' + 'for Promise don\'t want this'); //#2 }) // Promise } // […]

混合asynchronous操作(callback和承诺)完成后如何执行任务?

我在写一些nodejs应用程序。 我的应用程序有一些npm模块的依赖。 在一个任务中,涉及两个asynchronous操作,两个asynchronous操作是以callback和承诺的forms。 下面我把一些示例代码: // the task which should be performed after async operation var myTask = function(){ // do something } // first async operation in the form of callback cbapi(myTask) // second async operation in the form of promise promiseapi() .then(myTask) 在上例中,当第一次asynchronous和第二次asynchronous操作完成时, myTask将执行两次。 但是,我想要的是只有在两个asynchronous操作完成后才执行一次。 有没有办法做到这一点?

悬挂callback:在每个callback已经返回之前返回响应

问题:你认为悬挂callback是不好的node.js风格,甚至是危险的吗? 如果是这样的前提下呢? 案例:如下所述,假设您需要在更新某些数据的快速服务器上调用数据库。 然而,客户不需要被告知结果。 在这种情况下,您可以立即返回响应, 而不是等待asynchronous调用完成。 这将被描述为没有更好的名字的悬挂callback 。 为什么这很有趣?:因为在大多数情况下教程和文档都显示等待的情况,在最糟糕的情况下教导callback地狱。 回想一下你说的快递,蒙古包和护照的第一次经历。 例: 'use strict' const assert = require('assert') const express = require('express') const app = express() function longOperation (value, cb) { // might fail and: return cb(err) …here setTimeout(() => { // after some time invokes the callback return cb(null, value) }, 4000) } app.get('/ping', function […]

nodejscallback使用module.exports

我从NodeJS开始,并试图了解callback如何与module.exports相关。 我有两个NodeJS文件。 s1.js接收input字段消息并将结果传回。 s1.js通过module.exports公开 start.js包含s1.js(通过require),并接收我想在程序中使用的结果 start.js var s1=require('./s1.js'); var r1; s1.test("Leon", function(err, result) { if (err){console.log("Error occurred "+err);} console.log("Callback after s1.test execution "+result); r1=result; } ); console.log("result "+r1); console.log("end"); s1.js module.exports.test=function test(msg,result){ result="Hello "+msg; console.log("Inside s1.js – "+result); }; 当我执行start.js时,结果如下 Inside s1.js – Hello Leon result undefined end 正如所料,结果是未定义的,因为s1.test的callback尚未完成。 我不明白的是为什么从来没有达到s1.test的callback。 有任何想法吗? 谢谢莱昂

如何正确使用Promise.all()和然后()与asynchronous函数?

在下面的代码中,我试图把第一个和第二个查询的结果放在一个名为result的全局variables中。 问题是Promise.all()没有等待查询完成, then() 。 我该如何解决? 码: var result = {}; Promise.all([ connection.query('SELECT * FROM analysis', function(err, rows, fields) { if (err) throw err; result.analysis = rows; console.log("query 1"); }), connection.query('SELECT * FROM analysis_description', function(err, rows, fields) { if (err) throw err; result.analysis_description = rows; console.log("query 2"); }) ]) .then(function(result){ console.log(result); console.log("result"); }); 输出: result query […]

Express / MongoDB – 数据库操作期间的意外行为

我有一个用户列表,我试图存储/更新到我的mongoDB数据库。 但是只有我列表中的最后一个元素被考虑在内,其他用户则不能进入thenfunction。 我是Nodejs的新手,这似乎是由于我不明白的同步行为。 var User = require('../to/path/user-service'); …. userList.forEach(function(user) { try { User.updateUser(user); } catch(err) { return console.error(err); } }); 用户服务 … var User = mongoose.model('User', userSchema); console.log(user.name); export.updateUser = function (user) { Issue.findOne({name: user.name}) .then((user) => { if(user) console.log(user.name + ' has been updated'); else console.log(user.name + ' has been created'); }).catch((err) => console.error(err)); […]