Parse-Server - 无法从远程机器上的数据库中获取数据
Posted
技术标签:
【中文标题】Parse-Server - 无法从远程机器上的数据库中获取数据【英文标题】:Parse-Server - Cannot get data from DB on a remote machine 【发布时间】:2019-01-30 19:41:51 【问题描述】:我正在使用 Parse-Server,并且我在 C# 和 Swift 上开发了软件,使用适当的 SKD 远程从该服务器获取数据到每种编程语言,没有任何问题。
但现在我需要使用浏览器获取数据,但从远程计算机获取数据时遇到了问题。我可以在安装 Parse-Server 的计算机上使用 localhost 获取它。
我正在使用 parse sdk for 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>
这里是 Parse-Server 初始文件 (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);
这是我从浏览器收到的错误:
谷歌浏览器:
发布http://localhost:1337/parse/classes/Internacao 网络::ERR_CONNECTION_REFUSED
Mozilla Firefox:
跨域请求被阻止:同源策略不允许读取 http://localhost:1337/parse/classes/Internacao 的远程资源。 (原因:CORS 请求未成功)。
我已经下载了 cors() 并在应用声明后使用它,但它仍然不起作用。
[...]
var app = express();
app.use(cors());
[...]
我已尝试创建路线和 cors 选项,但没有任何变化。
【问题讨论】:
【参考方案1】:我改变了这一行
Parse.serverURL = "http://localhost:1337/parse";
到
Parse.serverURL = "http://[SERVER-IP]:1337/parse";
它现在正在工作。
【讨论】:
以上是关于Parse-Server - 无法从远程机器上的数据库中获取数据的主要内容,如果未能解决你的问题,请参考以下文章
Flutter parse-server sdk 无法删除对象