如何通过Cloud Code访问Parse Server中的数据?

时间:2017-03-06 11:18:56

标签: apache parse-platform xmlhttprequest cloud-code

我尝试使用Parse服务器的云代码功能进行查询。遗憾的是,我们无法从数据库中检索任何数据。我们的代码如下:

main.js:

Parse.Cloud.define('test', function(request, response) {
var user = request.user;
var token = user.getSessionToken();
var query = new Parse.Query('Carpark');
query.first({ sessionToken: token }) // pass the session token to find()
.then(function(messages) {
response.success(messages);
}, function(error) {
response.error(error);
});
});

index.js:

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

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

var api = new ParseServer({
databaseURI: 'mongodb://parse-server:[...]@localhost:27017/[...]',
cloud: __dirname + '/cloud/main.js',
appId: '[...]',
masterKey: '[...], //Add your master key here. Keep it secret!
serverURL: 'https://backend.[...]/parse', // Don't forget to change to https if needed
publicServerURL: 'https://backend.[...]/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();

//var basicAuth = require('basic-auth-connect');
//app.use(basicAuth('triveme', 'triveme'));

app.use('/', express.static(path.join(__dirname, '/public')));
app.use('/parse', api);
app.get('/test', function(req, res) {
res.sendFile(path.join(__dirname, '/public/test.html'));
});

var port = 61004;
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);

来自iOS-App的示例请求:

PFCloud.callFunction(inBackground: "test", withParameters: nil) {
(response, error) -> Void in
if let response = response {
let result = response
print("Cloud data:", result )
}
if error != nil {
print(error ?? "default cloud function error")
}
}

我没有从服务器获得任何反馈(没有回复也没有错误)。我的问题可能出现什么问题?

使用verbose = 1的日志请求:

REQUEST for [POST] /parse/functions/test: {} method=POST, url=/parse/functions/test, host=localhost:61004, accept=*/*, x-parse-session-token=[...], x-parse-application-id=[...].platform.dev2, x-parse-installation-id=[...], x-parse-os-version=10.2 (16D32), accept-language=en-us, accept-encoding=gzip, deflate, x-parse-client-version=i1.14.2, user-agent=trive.park/8 CFNetwork/808.2.16 Darwin/16.4.0, x-parse-app-build-version=8, x-parse-app-display-version=1.0, x-forwarded-for=[...], x-forwarded-host=backend.[...], x-forwarded-server=backend.[...], connection=Keep-Alive, content-length=0,

日志响应:

4|trive-pa | error: Failed running cloud function test for user nZ76ZimELw with:
4|trive-pa |   Input: {}
4|trive-pa |   Error: {"code":141,"message":{"code":100,"message":"XMLHttpRequest failed: \"Unable to connect to the Parse API\""}} functionName=test, code=141, code=100, message=XMLHttpRequest failed: "Unable to connect to the Parse API",  user=nZ76ZimELw
4|trive-pa | error: Error generating response. ParseError {
4|trive-pa |   code: 141,
4|trive-pa |   message: 
4|trive-pa |    ParseError {
4|trive-pa |      code: 100,
4|trive-pa |      message: 'XMLHttpRequest failed: "Unable to connect to the Parse API"' } } code=141, code=100, message=XMLHttpRequest failed: "Unable to connect to the Parse API"
4|trive-pa | [object Object]

parse-server version:2.2.23 自托管在Apache Server上 MongoDB的

0 个答案:

没有答案