gpt4 book ai didi

javascript - mLab : Find all users getting db object instead of records

转载 作者:太空宇宙 更新时间:2023-11-04 02:13:32 25 4
gpt4 key购买 nike

我的快速应用程序正在使用 mLab 数据库,如果我只找到一条记录,那就没问题,但是当我要从数据库中查找用户列表时,它会返回类似这样的内容。

Query
let User = app.db.collection("User");
User.find((err, users) => {
if (err) { return err; }
if (users) {
return res.json({ users: users});
}
});

我收到类似的回复

Readable {  
pool: null,
server: null,
disconnectHandler: { s: { storedOps: [], storeOptions: [Object], topology: [Object] },
length: [Getter] }, bson: {}, ns: 'hs-cloud.User', cmd: { find: 'hs-cloud.User',
limit: 0,
skip: 0,
query: {},
slaveOk: true,
readPreference: { preference: 'primary', tags: undefined, options: undefined } }, options: { skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: { preference: 'primary', tags: undefined, options: undefined },
db:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter] },
promiseLibrary: [Function: Promise],
disconnectHandler: { s: [Object], length: [Getter] } }, topology: EventEmitter {
domain: null,
_events:
{ reconnect: [Function],
timeout: [Object],
error: [Object],
close: [Function],
destroy: [Object],
serverDescriptionChanged: [Function],
serverHeartbeatStarted: [Function],
serverHeartbeatSucceeded: [Function],
serverHearbeatFailed: [Function],
serverOpening: [Function],
serverClosed: [Function],
topologyOpening: [Function],
topologyClosed: [Function],
topologyDescriptionChanged: [Function] },
_eventsCount: 14,
_maxListeners: undefined,
s:
{ options: [Object],
callbacks: [Object],
logger: [Object],
state: 'connected',
reconnect: true,
reconnectTries: 30,
reconnectInterval: 1000,
emitError: true,
currentReconnectRetry: 30,
ismaster: [Object],
readPreferenceStrategies: undefined,
authProviders: [Object],
id: 3,
topologyId: -1,
tag: undefined,
disconnectHandler: [Object],
monitoring: false,
haInterval: 10000,
wireProtocolHandler: {},
Cursor: [Object],
bsonInstance: {},
inquireServerStateTimeout: null,
bson: {},
pool: [Object],
isMasterLatencyMS: 138,
inTopology: false,
serverDetails: [Object],
serverDescription: null,
topologyDescription: null },
hashedName: '1309e35791f04f1f4fc35d5e683e81d350dd04f4',
name: [Getter],
bson: [Getter],
wireProtocolHandler: [Getter],
id: [Getter] }, cursorState: { cursorId: null,
cmd:
{ find: 'hs-cloud.User',
limit: 0,
skip: 0,
query: {},
slaveOk: true,
readPreference: [Object] },
documents: [],
cursorIndex: 0,
dead: false,
killed: false,
init: false,
notified: false,
limit: 0,
skip: 0,
batchSize: 1000,
currentLimit: 0,
transforms: undefined }, callbacks: null, logger: { className: 'Cursor' }, _readableState: ReadableState {
objectMode: true,
highWaterMark: 16,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null }, readable: true, domain: null, _events: {}, _eventsCount: 0, _maxListeners: undefined, s: { numberOfRetries: 5,
tailableRetryInterval: 500,
currentNumberOfRetries: 5,
state: 0,
streamOptions: {},
bson: {},
ns: 'hs-cloud.User',
cmd:
{ find: 'hs-cloud.User',
limit: 0,
skip: 0,
query: {},
slaveOk: true,
readPreference: [Object] },
options:
{ skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: [Object],
db: [Object],
promiseLibrary: [Function: Promise],
disconnectHandler: [Object] },
topology:
EventEmitter {
domain: null,
_events: [Object],
_eventsCount: 14,
_maxListeners: undefined,
s: [Object],
hashedName: '1309e35791f04f1f4fc35d5e683e81d350dd04f4',
name: [Getter],
bson: [Getter],
wireProtocolHandler: [Getter],
id: [Getter] },
topologyOptions:
{ socketOptions: {},
auto_reconnect: true,
host: 'ds030829.mlab.com',
port: 30829,
cursorFactory: [Object],
reconnect: true,
emitError: true,
size: 5,
disconnectHandler: [Object],
bson: {},
messageHandler: [Function],
wireProtocolHandler: {} },
promiseLibrary: [Function: Promise],
currentDoc: null }, timeout: false, sortValue: undefined
}

如果我尝试查找单个记录,它会返回具有匹配条件的记录

User.findOne({ username: 'abc123' }, (err, user) => {
if (err) { return next(err); }
if (!user) {
return next(null, false, {
message: "Incorrect username."
});
}

我已经像这样配置了 mongo 连接

return new Promise((resolve, reject) => {

MongoClient.connect('mongodb://<username>:<password>@ds033829.mlab.com:33829/my-cloud', (err, database) => {
let db = database;
if (err) {
return reject(err);
}
return resolve(db);
});

});

我将这个数据库存储到我的 Express 应用程序中就像

  • app.db = db;

friend 们,我错过了什么,我看到 mLab 文档,查询是相同的,但尽管我得到了类似的东西。

谢谢。

最佳答案

看起来好像 findOne() 返回一个文档,但 find() 返回一个光标。

 var cursor = User.find();
// Execute the each command, triggers for each document
cursor.forEach(function(doc) {
console.log(doc);
}, function(err) {
console.log(err);
});

关于javascript - mLab : Find all users getting db object instead of records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36882325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com