我正在尝试通过将Raspberry Pi 2与DietPi(DietPi v6.22.3),mongodb和nodejs结合使用,通过简单的mongo选择构建实时图表。
我想做的是使用类似的东西从数据库中获取数据:
var express = require('express');
var app = express();
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/DB_A';
var str = "";
app.route('/PAGE_A').get(function (req, res) {
MongoClient.connect(url, function (err, db) {
if (err) {
console.log(err);
} else {
console.log("Connected to db");
var cursor = db.collection('COL_A').find();
console.log(cursor);
//noinspection JSDeprecatedSymbols
cursor.each(function (err, item) {
if (item != null) {
str = str + " DateTime " + item.dt + " - Users: " + item.users + " </br>";
}
});
res.send(str);
db.close();
}
});
});
var server = app.listen(8080, function () {});
运行节点app.js并访问指定的IP:8080 / page_A,我收到“ Connected to db”消息,但没有打印来自游标的预期数据,除了我用于此问题的参数外。 集合不为空。我可以使用DB_A并从db.COL_A.find()中获取元素。
这是我“ npm start”并访问IP / PAGE_A时在控制台上打印的内容:
Connected to db
Cursor {
db:
Db {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
databaseName: 'DB_A',
serverConfig:
Server {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
_callBackStore: [CallbackStore],
host: 'localhost',
port: 27017,
options: [Object],
internalMaster: true,
connected: true,
poolSize: 5,
disableDriverBSONSizeCheck: false,
slaveOk: undefined,
_used: true,
replicasetInstance: null,
ssl: false,
sslValidate: false,
sslCA: null,
sslCert: undefined,
sslKey: undefined,
sslPass: undefined,
_readPreference: null,
socketOptions: [Object],
logger: [Object],
eventHandlers: [Object],
_serverState: 'connected',
_state: [Object],
recordQueryStats: false,
db: [Circular],
dbInstances: [Array],
connectionPool: [EventEmitter],
isMasterDoc: [Object] },
options:
{ read_preference_tags: null,
read_preference: 'primary',
native_parser: false,
readPreference: [ReadPreference],
safe: false,
w: 1 },
_applicationClosed: false,
native_parser: false,
bsonLib:
{ Code: [Function: Code],
Symbol: [Function: Symbol],
BSON: [Function],
DBRef: [Function: DBRef],
Binary: [Function],
ObjectID: [Function],
Long: [Function],
Timestamp: [Function],
Double: [Function: Double],
MinKey: [Function: MinKey],
MaxKey: [Function: MaxKey] },
bson: BSON {},
bson_deserializer:
{ Code: [Function: Code],
Symbol: [Function: Symbol],
BSON: [Function],
DBRef: [Function: DBRef],
Binary: [Function],
ObjectID: [Function],
Long: [Function],
Timestamp: [Function],
Double: [Function: Double],
MinKey: [Function: MinKey],
MaxKey: [Function: MaxKey] },
bson_serializer:
{ Code: [Function: Code],
Symbol: [Function: Symbol],
BSON: [Function],
DBRef: [Function: DBRef],
Binary: [Function],
ObjectID: [Function],
Long: [Function],
Timestamp: [Function],
Double: [Function: Double],
MinKey: [Function: MinKey],
MaxKey: [Function: MaxKey] },
_state: 'connected',
pkFactory:
{ [Function: ObjectID]
index: 0,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString] },
forceServerObjectId: false,
safe: false,
notReplied: {},
isInitializing: true,
auths: [],
openCalled: true,
commands: [],
logger:
{ error: [Function: error],
log: [Function: log],
debug: [Function: debug] },
slaveOk: false,
tag: 1554299599878,
eventHandlers:
{ error: [],
parseError: [],
poolReady: [],
message: [],
close: [] },
serializeFunctions: false,
raw: false,
recordQueryStats: false,
retryMiliSeconds: 1000,
numberOfRetries: 60,
readPreference:
ReadPreference { _type: 'ReadPreference', mode: 'primary', tags: undefined } },
collection:
Collection {
db:
Db {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
databaseName: 'DB_A',
serverConfig: [Server],
options: [Object],
_applicationClosed: false,
native_parser: false,
bsonLib: [Object],
bson: BSON {},
bson_deserializer: [Object],
bson_serializer: [Object],
_state: 'connected',
pkFactory: [Function],
forceServerObjectId: false,
safe: false,
notReplied: {},
isInitializing: true,
auths: [],
openCalled: true,
commands: [],
logger: [Object],
slaveOk: false,
tag: 1554299599878,
eventHandlers: [Object],
serializeFunctions: false,
raw: false,
recordQueryStats: false,
retryMiliSeconds: 1000,
numberOfRetries: 60,
readPreference: [ReadPreference] },
collectionName: 'COL_A',
internalHint: null,
opts: {},
slaveOk: false,
serializeFunctions: false,
raw: false,
readPreference: 'primary',
pkFactory:
{ [Function: ObjectID]
index: 0,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString] } },
selector: {},
fields: undefined,
skipValue: 0,
limitValue: 0,
sortValue: undefined,
hint: null,
explainValue: undefined,
snapshot: undefined,
timeout: true,
tailable: undefined,
awaitdata: undefined,
numberOfRetries: 5,
currentNumberOfRetries: 5,
batchSizeValue: 0,
slaveOk: false,
raw: false,
read: 'primary',
returnKey: undefined,
maxScan: undefined,
min: undefined,
max: undefined,
showDiskLoc: undefined,
comment: undefined,
tailableRetryInterval: 100,
exhaust: false,
partial: false,
totalNumberOfRecords: 0,
items: [],
cursorId: Long { _bsontype: 'Long', low_: 0, high_: 0 },
dbName: undefined,
state: 0,
queryRun: false,
getMoreTimer: false,
collectionName: 'DB_A.COL_A' }
以下是我的设置的一些详细信息:
mongod --version
db version v2.4.14
node --version
v10.15.3
NodeJS mongo driver -> 1.2.13
cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
cat /etc/debian_version
9.8
uname -a
Linux DietPi 4.14.98+ #1200 Tue Feb 12 20:11:02 GMT 2019 armv6l GNU/Linux
cat /proc/cpuinfo
processor : 0
model name : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS : 697.95
Features : half thumb fastmult vfp edsp java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xb76
CPU revision : 7
Hardware : BCM2835
Revision : 000e
Serial : 00000000ce0ee037
谢谢。
答案 0 :(得分:0)
如果您想要欲望结果,可以使用async await
MongoClient.connect(url, async (err, db) => {
if (err) {
console.log(err);
} else {
console.log("Connected to db");
var cursor = await db.collection("comments").find().toArray();
console.log({ cursor });
db.close();
}
});
但是,如果您想在每种游标方法中使用游标类型,则不确定为什么不起作用
还将mongodb版本"^3.2.2"
更改为"^2.2.33"
没事