小编Yam*_*ina的帖子

如何选择一个事件监听器,让我等到async.times完成后才能运行一个函数

我正在使用node.js服务器,Spotify API和spotify-web-api-js节点模块来创建一个Web应用程序,用户可以在其中输入艺术家的名字,查看相关艺术家的歌曲列表,然后可选择将该播放列表保存到自己的Spotify帐户.但是,我仍然在最后一步遇到麻烦.

我的用户授权流程首先发生:

if (params.access_token) {

    s.setAccessToken(params.access_token);

    s.getMe().then(function(data) {

      console.log(data);
      console.log(data.id);
      user_id = data.id;
Run Code Online (Sandbox Code Playgroud)

下面是API收集歌曲的实际细节的地方.尽管它较低,但它首先出现,并且仅当用户单击该页面上的第二个按钮时才会发生用户授权.

async.times(counter, function(n, next){
        s.getArtistTopTracks(relatedArtists[n].id, "US", function (err, data2) {
          relatedArtists[n].song = data2.tracks[0].name;
          relatedArtists[n].uri = data2.tracks[0].uri;
          console.log(relatedArtists[n].uri);
          // make sure to put the access token here add song to playlist
          // create array
          song_uris.push(relatedArtists[n].uri);
          console.log(song_uris);

          // song_uris = relatedArtists[n].uri;
          //
          // console.log(song_uris);

          next(null);

      $("#playlist").load(function() {
            s.addTracksToPlaylist(user_id, playlist_id, song_uris);
          });
        });


      }, function(err) {
        // console.table(relatedArtists);

        for (k = 0; k < 20; k++)
        { …
Run Code Online (Sandbox Code Playgroud)

javascript jquery asynchronous addeventlistener spotify

13
推荐指数
1
解决办法
253
查看次数

Rails/Devise登录在Safari上不起作用(422/CSRF错误)

登录在Chrome上运行正常,但在Safari(或我假设其他Webkit浏览器)上不起作用.登录后我收到此错误消息("您想要的更改被拒绝.也许您尝试更改了您无法访问的内容."):

你想要的改变被拒绝了. 也许你试图改变一些你无法访问的东西.

根据我的heroku日志,这就是发生的事情:

2016-12-07T14:14:23.778153+00:00 app[web.1]: Can't verify CSRF token authenticity
2016-12-07T14:14:23.778899+00:00 app[web.1]: Completed 422 Unprocessable Entity in 2ms (ActiveRecord: 0.0ms)
2016-12-07T14:14:23.785544+00:00 app[web.1]:
2016-12-07T14:14:23.785547+00:00 app[web.1]: ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
Run Code Online (Sandbox Code Playgroud)

我相信我正在发送正确的CSRF令牌,但似乎有些事情发生了故障.这是我目前的application_controller.rb:

class ApplicationController < ActionController::Base


  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  after_action :flash_to_headers

  # this is so that json requests don't redirect without a user
  before_action :authenticate_user!
  # before_action :authenticate_user!, unless: request.format == :json
  # before_action :user_needed, if: …
Run Code Online (Sandbox Code Playgroud)

safari ruby-on-rails csrf devise

12
推荐指数
1
解决办法
1750
查看次数

Postgres不会使用c9.io连接到Ruby on Rails应用程序中的服务器

几天前,我能够成功安装Postgresql并从SQLite创建/迁移我的数据库(准备部署我的Rails 4应用程序).​​..或者我认为.

我重新启动了我的服务器,但当我尝试访问我的应用程序时,出现此错误:

PG::ConnectionBad
could not connect to server: Connection refused 
Is the server running locally and accepting connections on Unix domain socket 
"/var/run/postgresql/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)

我在SO上看过几个类似的回答问题,但它们都涉及Mac.由于我正在使用Cloud 9 IDE,我认为可能存在差异(例如,提及localhost可能不相关,因为这不是我访问当前在c9.io上的应用程序的方式.)

根据其他问题的答案,我在我的终端尝试了这个:

sudo ps auxw | grep post
Run Code Online (Sandbox Code Playgroud)

这给了我:

ubuntu    2527  0.0  0.0  10552   888 pts/5    S+   15:21   0:00 grep --color=auto post
Run Code Online (Sandbox Code Playgroud)

然后我试过:

sudo find / -name .s.PGSQL.5432 -ls
Run Code Online (Sandbox Code Playgroud)

我得到了:

5286    0 lrwxrwxrwx   1 root     root           26 May 11 15:13 /var/pgsql_socket/.s.PGSQL.5432 -> /private/tmp/.s.PGSQL.5432
5183    0 srwxrwxrwx   1 postgres postgres        0 May 10 …
Run Code Online (Sandbox Code Playgroud)

ruby postgresql ruby-on-rails cloud9-ide

7
推荐指数
1
解决办法
4055
查看次数

允许cors jQuery POST请求Express.js服务器上的Spotify API

为了在我的Web应用程序中获取Spotify API的访问令牌(由他们的Web授权流程指定),我了解到我必须提出POST请求.但是,当我这样做时,我得到了XMLHttpRequest 500 Error由于跨源问题.

我已经想出了如何允许CORS GET请求,但我不知道如何对POST请求执行相同的操作.此链接提供配置提示,但它保留实际路由GETPOST空白.

这是我的Express.js服务器的相关代码:

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});



app.use(express.static(__dirname + '/public')); // looks in public directory, not root directory (protects files)

app.get('/', function(req, res) {
  // res.header("Access-Control-Allow-Origin", "*");
  // res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.send(__dirname + '\\index.html')
});

app.post('/', function(req, res) {
    res.send(req.body.spotify);
});
Run Code Online (Sandbox Code Playgroud)

(spotifyspotify-web-api-js节点模块).

我以前曾尝试将确切的代码复制app.getapp.post,但这导致服务器崩溃.

这是我程序的JavaScript文件中的一些代码,它打算POST在用户单击一个按钮后发送请求,该按钮将它们带到Spotify的授权路径的开头并批准登录:

$('#spotify').on('click', …
Run Code Online (Sandbox Code Playgroud)

jquery spotify node.js cors express

0
推荐指数
1
解决办法
3250
查看次数