mysql-xdevapi getCollections 承诺 return 值
mysql-xdevapi getCollections promise return value
我一直在尝试在我的数据库架构中输出集合数组。
假设我的会话有效,我会得到一个 Promise{} 输出,我认为这是正常的。
.then(()=>{
return sess.getSchema('mySchema').getCollection('myCollection')
.find().fields(['name','age'])
.execute(row=>{
console.log(row)
})
})
.then(()=>{
let r = sess.getSchema('mySchema').getCollections()
console.log(r)
return r
})
但如果我尝试获取 promise 中的值
let r = sess.getSchema('mySchema').getCollections()
r.then(v=>{
console.log(v)
})
它returns我这些会话回调函数
[
{
getSession: [Function: getSession],
add: [Function: add],
addOrReplaceOne: [Function: addOrReplaceOne],
count: [Function: count],
existsInDatabase: [Function: existsInDatabase],
find: [Function: find],
getName: [Function: getName],
getSchema: [Function: getSchema],
inspect: [Function: inspect],
modify: [Function: modify],
remove: [Function: remove],
removeOne: [Function: removeOne],
replaceOne: [Function: replaceOne],
dropIndex: [Function: dropIndex],
createIndex: [Function: createIndex],
getOne: [Function: getOne]
}
]
这就是 API 的工作方式。 getCollections()
方法 returns 一个 Collection
实例数组。每个实例都有一组特定的方法。
因此,例如,如果您想获取集合名称,您可以这样做:
sess.getSchema('mySchema').getCollections()
.then(collections => {
console.log(collections.map(c => c.getName()))
})
免责声明:我是 MySQL X DevAPI Connector for Node.js
的首席开发人员
我一直在尝试在我的数据库架构中输出集合数组。 假设我的会话有效,我会得到一个 Promise{} 输出,我认为这是正常的。
.then(()=>{
return sess.getSchema('mySchema').getCollection('myCollection')
.find().fields(['name','age'])
.execute(row=>{
console.log(row)
})
})
.then(()=>{
let r = sess.getSchema('mySchema').getCollections()
console.log(r)
return r
})
但如果我尝试获取 promise 中的值
let r = sess.getSchema('mySchema').getCollections()
r.then(v=>{
console.log(v)
})
它returns我这些会话回调函数
[
{
getSession: [Function: getSession],
add: [Function: add],
addOrReplaceOne: [Function: addOrReplaceOne],
count: [Function: count],
existsInDatabase: [Function: existsInDatabase],
find: [Function: find],
getName: [Function: getName],
getSchema: [Function: getSchema],
inspect: [Function: inspect],
modify: [Function: modify],
remove: [Function: remove],
removeOne: [Function: removeOne],
replaceOne: [Function: replaceOne],
dropIndex: [Function: dropIndex],
createIndex: [Function: createIndex],
getOne: [Function: getOne]
}
]
这就是 API 的工作方式。 getCollections()
方法 returns 一个 Collection
实例数组。每个实例都有一组特定的方法。
因此,例如,如果您想获取集合名称,您可以这样做:
sess.getSchema('mySchema').getCollections()
.then(collections => {
console.log(collections.map(c => c.getName()))
})
免责声明:我是 MySQL X DevAPI Connector for Node.js
的首席开发人员