【发布时间】:2019-08-08 05:50:20
【问题描述】:
我正在为承诺、异步/等待而苦苦挣扎。在这里,我想将 gridfs 对象导出到另一个文件,但是当我使用 require 导入它并执行 console.log(gridfs) 时,它会给出一个空对象。任何人都可以帮助我如何导出 gridfs
const mongoose = require('mongoose');
async ()=> {
await mongoose.connection.on('connected', ()=>{
const gridfs = require('mongoose-gridfs')({
collection: 'sharedfiles',
model: 'SharedFiles',
mongooseconnection: mongoose.connection
});
global.sharedfile = gridfs;
});
module.exports = sharedfile;
}
我需要gridfs的文件:
const sharedfile = require('path to above file');
//under another promise
rslt.data.on('end', ()=>{
console.log(sharedfile);
}
我得到的结果是 {},因为 console.log 在脚本加载之前运行,任何人都可以建议如何修复它。 (我是 Promise 和 async/await 的新手)。
【问题讨论】:
-
mongoose.connection.on真的返回一个 Promise并接受回调吗? -
mongoose.connection.on 返回一个对象并接受回调@Cer
-
该对象是 Promise 吗?
-
我不确定,但我认为这是因为它可以由异步函数 @Cert 返回