代码之家  ›  专栏  ›  技术社区  ›  GuiDupas

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

  •  0
  • GuiDupas  · 技术社区  · 6 年前

    我使用的是Parse Server,我的软件是在C#和Swift上开发的,使用适当的SKD将数据远程从该服务器获取到每种编程语言,没有任何问题。

    但现在我需要使用浏览器获取数据,而我在从远程计算机获取数据时遇到了问题。我只需要在安装了解析服务器的计算机上使用localhost就可以得到它。

    我正在使用javascript解析sdk。

    这是我的代码(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>
    

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

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

    邮递 http://localhost:1337/parse/classes/Internacao 网络::错误连接被拒绝

    Mozila Firefox:

    已阻止跨源请求:同一源策略不允许读取 http://localhost:1337/parse/classes/Internacao (原因:CORS请求未成功)。

    我下载了cors()并在应用程序声明之后使用了它,但它仍然不起作用。

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

    我尝试过创建路线和cors选项,但没有任何改变。

    1 回复  |  直到 6 年前
        1
  •  0
  •   GuiDupas    6 年前

    我换了这行

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

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

    现在它正在工作。

    推荐文章