【问题标题】:"Unable to connect to the Parse API" using Parse Server on Heroku在 Heroku 上使用 Parse 服务器“无法连接到 Parse API”
【发布时间】:2017-02-12 04:47:26
【问题描述】:

当我尝试连接 Parse Server API 时,我收到错误 无法创建新对象,错误代码为:XMLHttpRequest failed:“无法连接到 Parse API”。我在 Heroku 上部署了 ParsePlatform/parse-server-example。我可以使用浏览器毫无问题地访问我的应用程序。尝试使用以下代码连接到 Heroku 上的 Parse 时出现错误:

var $result=$('#results').html('Testing configuration.....');
  Parse.initialize('<MY_APP_ID>', '<MY_JAVASRIPT_KEY>');
  Parse.serverURL = '<MY_HEROKU_APP_NAME>.herokuapp.com/'

  var ParseServerTest = Parse.Object.extend('ParseServerTest');
  var _ParseServerTest = new ParseServerTest();

  _ParseServerTest.set('key', 'value');
  _ParseServerTest.save(null, {
    success: function(_ParseServerTest) {
      var txt = 'Yay, your server works! New object created with objectId: ' + _ParseServerTest.id;
      $result.html('<div class="alert alert-success" role="alert">' + txt + '</div>');
    },
    error: function(_ParseServerTest, error) {
      var txt = 'Bummer, Failed to create new object, with error code: ' + error.message;
      $result.html('<div class="alert alert-danger" role="alert">' + txt + '</div>');
    }
  });

index.js

// Example express application adding the parse-server module to expose Parse
// compatible API routes.

var express = require('express');
var cors = require('cors');

var ParseServer = require('parse-server').ParseServer;
var path = require('path');

var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;

if (!databaseUri) {
  console.log('DATABASE_URI not specified, falling back to localhost.');
}

var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'myAppId',
  masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || 'https://localhost:1337/parse',  // Don't forget to change to https if needed
  liveQuery: {
    classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
  }
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey

var app = express();
app.use(cors());

// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));

// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);

// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
  res.status(200).send('I dream of being a website.  Please star the parse-server repo on GitHub!');
});

// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function(req, res) {
  res.sendFile(path.join(__dirname, '/public/test.html'));
});

var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
    console.log('parse-server-example running on port ' + port + '.');
});

// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

Heroku 配置:

我关注了这篇文章:How can I host my own Parse Server on Heroku using MongoDB?,除了我没有使用“部署到 Eroku”按钮,我手动部署了它。

感谢您的帮助。

【问题讨论】:

    标签: mongodb heroku parse-platform parse-server


    【解决方案1】:

    我终于找到了办法。 我首先在我的 mongo db 中创建了另一个用户,然后在 Heroku 中对其进行了更改。尝试用相同的js代码code jsfiddle连接,但没有成功...

    然后我尝试了一个android客户端,这个链接对我帮助很大http://www.robpercival.co.uk/parse-server-on-heroku/

    StarterApplication.java

    public class StarterApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
    
        // Enable Local Datastore.
        Parse.enableLocalDatastore(this);
    
        // Add your initialization code here
        Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
                .applicationId("BUTYcVjD7nFz4Le")
                .clientKey("XgQaeDY8Bfvw2r8vKCW")
                .server("https://xxxxx-xxxx-xxxxx.herokuapp.com/parse")
                .build()
        );
    
        ParseUser.enableAutomaticUser();
        ParseACL defaultACL = new ParseACL();
        // Optionally enable public read access.
        // defaultACL.setPublicReadAccess(true);
        ParseACL.setDefaultACL(defaultACL, true);
    }
    

    MainActivity.java

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    
        ParseAnalytics.trackAppOpenedInBackground(getIntent());
    
        ParseObject test = new ParseObject("Test");
        test.put("username","pedro");
        test.put("age",33);
        test.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                if (e == null) {
                    Log.i("Parse", "Save Succeeded");
                } else {
                    Log.e("Parse", "Save Failed");
                }
            }
        });
    
    }
    

    我真的不知道我的第一个用户有什么问题,无法连接。我永远无法连接 js 代码......但无论如何我的目标是连接 Android 客户端所以......

    【讨论】:

      猜你喜欢
      • 2017-04-10
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多