小编qui*_*bit的帖子

阅读获取承诺的主体

我确信这有一个简单的答案,但对于我的生活,我无法弄清楚如何去做.

我有以下用于上传到Google云端存储的快速端点.它工作得很好,谷歌api的响应给了我一个独特的文件名,我想传回我的前端:

app.post('/upload', (req, res) => {
  var form = new formidable.IncomingForm(),
  files = [],
  fields = [];

  form
    .on('field', function(field, value) {
      fields.push([field, value]);
    })
    .on('file', function(field, file) {
      files.push([field, file]);
    })
    .on('end', function() {
      console.log('-> upload done');
    });
  form.parse(req, function(err, fields, files){
    var filePath = files.file.path;
    bucket.upload(filePath, function(err, file, apiResponse){
      if (!err){
        res.writeHead(200, {'content-type': 'text/plain'});
        res.end("Unique File Name:" + file.name);
      }else{
        res.writeHead(500);
        res.end();
      }
    });
  });

 return;
});
Run Code Online (Sandbox Code Playgroud)

我通过调用一个将文件传递给它的短函数来到达此端点:

function upload(file) {
  var data = new FormData();
  data.append('file', …
Run Code Online (Sandbox Code Playgroud)

javascript node.js fetch-api

38
推荐指数
4
解决办法
3万
查看次数

使用Google Cloud Service Account密钥文件中的内容设置Heroku Config Var

我有一个我正在尝试使用Heroku部署的Web应用程序.我正在使用Google Cloud Node.js库来访问Google云端存储.我的服务器文件顶部有以下内容:

const gcs = require('@google-cloud/storage')({
  projectId: 'my-project-ID',
  credentials: process.env.GCS_KEYFILE
});
Run Code Online (Sandbox Code Playgroud)

GCS_KEYFILE是我使用Heroku命令行工具设置的配置变量 heroku config:set GCS_KEYFILE="$(< /my/file.json)"

检查仪表板以确保已确认JSON文件的内容已设置为config var.屏幕截图如下所示: 在此输入图像描述

我尝试做任何事情时得到的错误gcs是:

Error: Could not authenticate request 
The incoming JSON object does not contain a client_email field
Run Code Online (Sandbox Code Playgroud)

这没有任何意义,因为它显然有效.json本身很好; 我直接从Google Cloud下载了该下载而未对其进行修改.我已经尝试keyFileName在我的const gcs声明中使用但是我收到了一个ENAMETOOLONG错误(加上文档说明了用于指定JSON文件的路径).它在我使用时在本地工作keyFileName并指定JSON文件的路径,所以我很确定JSON本身不是问题.

有关为什么我收到此错误的任何想法?或者是否有更好的方法在Heroku上处理来自Google的JSON Keyfiles?

heroku node.js google-cloud-platform

5
推荐指数
1
解决办法
859
查看次数

在 React 中管理多个音频源

我有一个 React 组件,可以在您单击按钮时播放/暂停音频。它工作得很好,我一次在一个页面上渲染了大约 5 个。但是,如果您单击一个播放,然后单击另一个播放,两个音频都在播放,这不是很好。这是我的组件代码:

import React from 'react';
import playIcon from './images/play.png';
import pauseIcon from './images/pause.png';
class Music extends React.Component {
    constructor(props) {
        super(props);
        this.state = { 'play': false };
        this.url = props.src;
        this.audio = new Audio(this.url);
        this.audio.preload = 'none';
        this.togglePlay = this.togglePlay.bind(this);
    }

    togglePlay() {
        this.setState({'play': !this.state.play}, () => {
            this.state.play ? this.audio.play() : this.audio.pause();
        });
    }

    componentWillUnmount () {
        this.audio.pause();
    }

    render() {
        return (
            <div style={this.props.style} className={this.props.className}>
                {this.state.play
                    ? <button className="audio-button" aria-label="Pause" onClick={this.togglePlay}><img src={pauseIcon} width="34" height="34" …
Run Code Online (Sandbox Code Playgroud)

javascript audio reactjs

5
推荐指数
1
解决办法
2929
查看次数

如何按Activerecord中的计算值排序?

我有一个rails API,它将JSON返回到我的React前端.我正在尝试按集合中每个项目的计算值进行排序.我有一个Space具有area属性和count属性的模型.我想作为排序依据的响应total_area这仅仅是area * count.我可以使用这个,sort_by但即使记录少于100个,这个过程也很慢:

@spaces = Space.all
@spaces = @spaces.sort_by(&:total_area) 
Run Code Online (Sandbox Code Playgroud)

哪里total_area是一个Space类的方法:

def total_area
  self.area * self.count
end
Run Code Online (Sandbox Code Playgroud)

无论如何,在数据库中这样做是为了提高速度吗?我尝试过使用这个order方法:

@spaces.order( "count * area" => :asc)
Run Code Online (Sandbox Code Playgroud)

但我得到以下postgres错误:

PG::UndefinedColumn: ERROR:  column spaces.count * area does not exist
Run Code Online (Sandbox Code Playgroud)

可以在数据库中执行此操作吗?任何有关我如何能够提出的建议,或者我如何更快地做到这一点都将非常感激.

activerecord ruby-on-rails rails-activerecord

2
推荐指数
1
解决办法
1550
查看次数