我正在尝试使用与Bluebird库的promises重构我的nodejs服务器,但我陷入了一个简单的问题.
从我的数据库中获取用户之后,我想列出与该用户关联的所有通知类:
坏路(工作......)
adapter.getUsers(function(users){
users.rows.forEach(function(item){
user = item.username;
adapter.getNotifications(user, function(notificationList){
console.log(notificationList);
})
});
});
Run Code Online (Sandbox Code Playgroud)
优雅的暂定方式(不工作...)
var getNotifications = Promise.promisify(adapter.getNotifications);
adapter.getUsers().then(function(users) {
users.rows.forEach(function(item){
var dbUser = "sigalei/" + item.value.name;
console.log(dbUser);
return getNotifications(dbUser);
});
}).then(function(result){
console.log(result);
console.log("NOTIFICATIONLIST");
});
Run Code Online (Sandbox Code Playgroud)
但是,当我执行此代码时,我在getNotification方法中遇到此错误:
未处理的拒绝TypeError:无法在TryCatcher(/ Users/DaniloOliveira /)上的Adapter.getNotifications(/Users/DaniloOliveira/Workspace/sigalei-api/api/tools/couchdb-adapter.js:387:30)读取未定义的属性'nano'.工作区/ sigalei-API/node_modules /蓝鸟/ JS /主/ util.js中:26:23)
var Adapter = module.exports = function(config) {
this.nano = require('nano')({
url: url,
request_defaults: config.request_defaults
});
};
Adapter.prototype.getNotifications = function(userDb, done) {
var that = this;
console.log(that);
var userDbInstance = that.nano.use(userDb);
userDbInstance.view('_notificacao', 'lista',
{start_key: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用NodeJS/express 4创建一个端点,该端点生成并向用户发送xlsx文件.
要创建xlsx文件,我正在使用该node-xlsx库.
var xlsx = require('node-xlsx');
var buffer = xlsx.build([{
name: pasta,
data: data
}]);
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader("Content-Disposition", "attachment; filename=" + pasta + ".xlsx");
res.write(buffer, 'binary');
return res.end();
Run Code Online (Sandbox Code Playgroud)
我试图通过Angular应用程序下载此文件.
$http.post('https://endpoint/v1/' + folderName + '/reportExcel', {
responseType: 'arraybuffer'
})
.success(function(response) {
var blob = new Blob([response], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
});
var objectUrl = URL.createObjectURL(blob);
$window.open(objectUrl);
Run Code Online (Sandbox Code Playgroud)
但是,正在下载的文件已损坏,因此无法打开它.