[英]How does this weird search pattern (destructuring) work in MongoDB promises?
在mongo中關注這個問題后,我看到了引起我注意的事情(看看then()
方法)
// connect to mongo, use Mongo Client
mongoose.connect(MONGO_URI, {useMongoClient: true})
.then(({db: {databaseName}}) => console.log(`Connected to ${databaseName}`))
.catch(err => console.error(err));
我確實理解在mongoose
對象中有一個db
屬性,下面有兩個或三個級別,這就是我想要的databaseName
。
我的問題 :
謝謝
您正在看的是ES6破壞語法 ,而不是對象。
它的意思是:
.then()
db
屬性 db
以查找其中的databaseName
屬性 databaseName
提升到當前范圍(在本例中為您的函數范圍) 最深層的變形變量將在當前范圍內可用。 這是一個例子:
let { db: { databaseName } } = { db: { databaseName: 'ding' } } // now databaseName is available in the current scope console.log(databaseName) // prints "ding"
這與用於執行ES6模塊導入的語法相同:
// import the entire module into the current scope
import something from 'something'
// import only parts of the module into the current scope
import { InsideSomething } from 'something'
// some people also destructure after importing and entire module
const { InsideSomething, another: { InsideAnother } } = something;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.