Я пытался создать соединение с Azure Cosmos DB (mongo API) в NodeJS в ES6.Стили обратного вызова все еще сохранялись, потому что я не мог найти примеры в ES6 (но ES7).В db.js
export function ReadAll (dbName, collectionName) {
return ConnectToCollection(dbName, collectionName).then(collection => {
collection.find({}).then(docs => {
// docs is not undefined here
return docs
})
})
}
function ConnectToCollection (dbName, collectionName) {
return new Promise((resolve, reject) => {
mongoClient.connect(
PRIMARY_CONNECTION_STRING,
{ useNewUrlParser: true },
(err, client) => {
if (err) reject(new Error(err))
const db = client.db(dbName)
const collection = db.collection(collectionName)
resolve(collection)
}
)
})
}
, который был вызван в index.js
app.get('/users', (req, res) => {
ReadAll(DB_NAME, COLLECTION_NAME_TEMP).then(docs => {
// but docs here is undefined
...
})
})
Я все еще получаю неопределенный docs
тоже:
export function ReadAll (dbName, collectionName) {
return ConnectToCollection(dbName, collectionName).then(collection => {
// Even with a return here
return collection.find({}).toArray(docs => {
return docs
})
})
}
почему яполучить неопределенное docs
здесь?