我正在使用nodejs和mongoose,在我试图执行两个find()查询的文件中。在第二个查询中我使用第一个查询的结果,但我认为第二个查询正在执行第一个或其他事情正在发生,我不明白。我的代码如下:
var express = require('express');
var morgan = require('morgan');
var moment=require('moment');
var fs=require('fs');
var mongoose = require('mongoose');
var Match= require('./Match');// schema
var Info = require('./Info');// schema
var bodyparser = require('body-parser');
var app = express();
app.use(morgan('dev'));
var crypto = require('crypto');
var cl;
var cn;
var x;
var Base64Id = function() { };
Base64Id.prototype.getRandomBytes = function(bytes) {
var BUFFER_SIZE = 4096
var self = this;
bytes = bytes || 12;
if (bytes > BUFFER_SIZE) {
return crypto.randomBytes(bytes);
}
var bytesInBuffer = parseInt(BUFFER_SIZE/bytes);
var threshold = parseInt(bytesInBuffer*0.85);
if (!threshold) {
return crypto.randomBytes(bytes);
}
if (this.bytesBufferIndex == null) {
this.bytesBufferIndex = -1;
}
if (this.bytesBufferIndex == bytesInBuffer) {
this.bytesBuffer = null;
this.bytesBufferIndex = -1;
}
// No buffered bytes available or index above threshold
if (this.bytesBufferIndex == -1 || this.bytesBufferIndex > threshold) {
if (!this.isGeneratingBytes) {
this.isGeneratingBytes = true;
crypto.randomBytes(BUFFER_SIZE, function(err, bytes) {
self.bytesBuffer = bytes;
self.bytesBufferIndex = 0;
self.isGeneratingBytes = false;
});
}
// Fall back to sync call when no buffered bytes are available
if (this.bytesBufferIndex == -1) {
return crypto.randomBytes(bytes);
}
}
var result = this.bytesBuffer.slice(bytes*this.bytesBufferIndex, bytes*(this.bytesBufferIndex+1));
this.bytesBufferIndex++;
return result;
}
/**
* Generates a base64 id
*
* (Original version from socket.io <http://socket.io>)
*/
Base64Id.prototype.generateId = function () {
console.log("n");
// my first query
Match.matching.find({}, function (err, mat) {
console.log("s");
if (err)
return console.log("error");
//response.json(infos);
if(mat.length){
console.log("nnn");
h=0;
console.log(mat[0].toObject().name);
cn=(mat[0].toObject().name);
}
});
console.log("nn");
if(h==0){
console.log("name"+cn);
h++;
//my second query
Info.inform.find({"name":cn}, function (err, inf) {
console.log("ss");
if (err)
return console.log("error");
console.log(inf);
if(inf.length){
console.log("sss");
x=0;
console.log(inf[0].toObject().link);
cl=inf[0].toObject().link;
}
}).select({"link": 1, "_id": 0});
}
if(x==0)
{
x++;
console.log("in base"+cl);
return cl;
}
else if(x!=0)
{
var rand = new Buffer(15); // multiple of 3 for base64
if (!rand.writeInt32BE) {
return Math.abs(Math.random() * Math.random() * Date.now() | 0).toString()
+ Math.abs(Math.random() * Math.random() * Date.now() | 0).toString();
}
this.sequenceNumber = (this.sequenceNumber + 1) | 0;
rand.writeInt32BE(this.sequenceNumber, 11);
if (crypto.randomBytes) {
this.getRandomBytes(12).copy(rand);
} else {
// not secure for node 0.4
[0, 4, 8].forEach(function(i) {
rand.writeInt32BE(Math.random() * Math.pow(2, 32) | 0, i);
});
}
return rand.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
}
};
exports = module.exports = new Base64Id();
如果我第一次查询返回结果中的某些内容然后我使h = 0,那么如果h = 0则需要执行第二次查询。如果我的第二个查询在结果中返回一些内容,那么我使x = 0,如果x = 0,则需要返回作为第二个查询结果获得的id或者需要返回新的id。
控制台中的实际输出必须为:
n
s
nnn
1000
nn
name1000
ss
sss
B_VfTceWRWyqC0DHACBD
in baseB_VfTceWRWyqC0DHACBD
B_VfTceWRWyqC0DHACBD joined// this output will be generated from another file
B_VfTceWRWyqC0DHACBD ready to stream//this output will be generated from another file
我正在给出所有真实案例,但我得到的输出为:
n
nn
-- V6_mcwH2edz52pAGAAAA joined --
s
nnn
1000
-- V6_mcwH2edz52pAGAAAA is ready to stream --
它表明我的查询没有按顺序执行。我是这个概念的新手我理解如何摆脱这些问题。