NodeJS 同步调用
NodeJS sync call
我正在尝试使用 nodejs 从数据库中获取记录数,但问题是由于同步请求,我无法打印它们。当我尝试在函数内部打印它时,它打印正常,但在函数外部却没有。我知道这是由于 JS 的同步行为,但我想这样做(可能是异步的)。我是 Javascript 和 nodejs 的新手,所以请指导我该怎么做。这是代码。
router.post('/numofrecs', function(req, res) {
var db = new Db('nmydb', new Server('localhost', '27017'));
db.open(function (err, db) {
db.authenticate('', '', function (err, result) {
var url = 'mongodb://localhost:27017/nmydb';
client.connect(url, function (err, db) {
que = new Array();
var questioncol = db.collection('allquestions');
questioncol.find({}).count(function (err, data) {
rec = data;
console.log("Num of rec:"+rec);
//inside the connect function , it prints fine here
});
console.log('Num of rec:'+ rec);
//but it doesnot print here outside the connect function and just print "undefined"
由于您正在构建 HTTP 服务器,因此您不希望在您的服务器上进行任何同步操作,因为此类操作会导致您的服务器在同步操作期间对所有客户端无响应。相反,您应该在数据库查询的回调中完成所需的工作。
router.post('/numofrecs', function(req, res) {
var db = new Db('nmydb', new Server('localhost', '27017'));
db.open(function (err, db) {
db.authenticate('', '', function (err, result) {
var url = 'mongodb://localhost:27017/nmydb';
client.connect(url, function (err, db) {
var que = new Array();
var questioncol = db.collection('allquestions');
questioncol.find({}).count(function (err, data) {
console.log("Num of rec:"+data);
// do stuff with data here.
doStuff(data);
});
});
});
});
});
不要忘记 var
你的变量。
npm 安装-g xd-synchttp
const sync = require('xd-synchttp');
let content = "";
let post_data={'a':1,'b':2};
try{
content = sync.http_get('http://www.csdn.net',3);
console.log(sync.http_post('http://www.baidu.com',post_data,0));
}
catch(err){
console.log(err);
}
我正在尝试使用 nodejs 从数据库中获取记录数,但问题是由于同步请求,我无法打印它们。当我尝试在函数内部打印它时,它打印正常,但在函数外部却没有。我知道这是由于 JS 的同步行为,但我想这样做(可能是异步的)。我是 Javascript 和 nodejs 的新手,所以请指导我该怎么做。这是代码。
router.post('/numofrecs', function(req, res) {
var db = new Db('nmydb', new Server('localhost', '27017'));
db.open(function (err, db) {
db.authenticate('', '', function (err, result) {
var url = 'mongodb://localhost:27017/nmydb';
client.connect(url, function (err, db) {
que = new Array();
var questioncol = db.collection('allquestions');
questioncol.find({}).count(function (err, data) {
rec = data;
console.log("Num of rec:"+rec);
//inside the connect function , it prints fine here
});
console.log('Num of rec:'+ rec);
//but it doesnot print here outside the connect function and just print "undefined"
由于您正在构建 HTTP 服务器,因此您不希望在您的服务器上进行任何同步操作,因为此类操作会导致您的服务器在同步操作期间对所有客户端无响应。相反,您应该在数据库查询的回调中完成所需的工作。
router.post('/numofrecs', function(req, res) {
var db = new Db('nmydb', new Server('localhost', '27017'));
db.open(function (err, db) {
db.authenticate('', '', function (err, result) {
var url = 'mongodb://localhost:27017/nmydb';
client.connect(url, function (err, db) {
var que = new Array();
var questioncol = db.collection('allquestions');
questioncol.find({}).count(function (err, data) {
console.log("Num of rec:"+data);
// do stuff with data here.
doStuff(data);
});
});
});
});
});
不要忘记 var
你的变量。
npm 安装-g xd-synchttp
const sync = require('xd-synchttp');
let content = "";
let post_data={'a':1,'b':2};
try{
content = sync.http_get('http://www.csdn.net',3);
console.log(sync.http_post('http://www.baidu.com',post_data,0));
}
catch(err){
console.log(err);
}