解析服务器-无法从远程计算机上的数据库获取数据

时间:2019-01-30 19:41:51

标签: parse-server parse-javascript-sdk

我正在使用Parse-Server,并且我有使用C#和Swift开发的软件,可以使用适当的SKD到每种编程语言从该服务器远程获取数据。

但是现在我需要使用浏览器来获取数据,但是从远程计算机获取数据时遇到了问题。我可以在安装Parse-Server的计算机上使用localhost来获取它。

我正在使用sdk解析javascript。

这是我的代码(index.html):

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Teste</title>

    <script src="https://unpkg.com/parse/dist/parse.min.js"></script>
    <script>
        function executaScript() {
            console.log("Script")

            Parse.initialize("HG", "HGJSKey", "HGMasterKey");
            Parse.serverURL = "http://localhost:1337/parse";

            var etapasInternacao = Parse.Object.extend("EtapaInternacao");
            var innerQuery = new Parse.Query(etapasInternacao);
            innerQuery.notEqualTo("objectId", "o0rudDQzng");

            var interns = Parse.Object.extend("Internacao");
            var query = new Parse.Query(interns);
            query.include("paciente");
            query.include("etapa");
            query.ascending("numeroQuarto");
            query.matchesQuery("etapa", innerQuery);

            query.find().then((resultado) => {

                this.internacoes = []
                for(var indice in resultado) {
                    console.log(resultado[indice].get("paciente").get("nomeCompleto"))
                }
            }, (error) =>  {
                // error is an instance of parse.error.
                alert(error.message)
            });
        }
        document.onload = executaScript()
    </script>

  </head>
  <body>
    <h1>Script</h1>
  </body>
</html>

这是解析服务器初始文件(index.js)

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

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

var api = new ParseServer({
  databaseURI: 'mongodb://localhost:27017/HGDB',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'HG',
  masterKey: process.env.MASTER_KEY || 'HGMasterKey', //Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed

  clientKey: 'HGKey',
  javascriptKey: 'HGJSKey',
  restAPIKey: 'HGRestKey',
  dotNetKey: 'HGDotNetKey',
  fileKey: 'HGFileKey',

  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();

// 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);

这是我从浏览器收到的错误:

谷歌浏览器:

  

POST http://localhost:1337/parse/classes/Internacao   净:: ERR_CONNECTION_REFUSED

Mozila Firefox:

  

跨源请求被阻止:同源策略禁止阅读   http://localhost:1337/parse/classes/Internacao上的远程资源。   (原因:CORS请求未成功。)

我已经下载了cors()并在应用程序声明后立即使用它,但仍然无法正常工作。

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

我尝试创建路由和cors选项,但没有任何变化。

1 个答案:

答案 0 :(得分:0)

我更改了这一行

Parse.serverURL = "http://localhost:1337/parse";

Parse.serverURL = "http://[SERVER-IP]:1337/parse";

它现在正在工作。