如何在解析服务器中用node.js查询对象

问题描述:

我正在尝试使用节点从我的Parse服务器请求数据。 Parse的文档根本没有意义。有人可以请完成我的代码,以便我可以请求名为“Items”的类中的所有对象?如何在解析服务器中用node.js查询对象

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

var databaseUri = 'mongodb://xx'; 

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 || 'xx', 
    masterKey: process.env.MASTER_KEY || 'xx', //Add your master key here. Keep it secret! 
    serverURL: process.env.SERVER_URL || 'https://site.herokuapp.com/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(); 

// 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('lol'); 
}); 

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

这是所有documentation解析提供,但它不适用于我。我也试过this,但仍然没有结果。有人可以编辑我的代码,以便我可以使用云代码吗?最终,我想在浏览器中调用函数“hello”,并查看“Items”类的所有对象。

+0

“完成我的代码”的问题在这里并不是太好。您应该尝试将您的问题降至最小,可复制的版本。考虑具体问题,而不是广泛的要求。 –

+0

您的服务器是否成功运行?你在日志中看到什么错误? main.js是否确实存在于该目录中?请你给我们更多的工作。 – Cliffordwh

+0

谢谢@SnakeBlisken现在问题已解决。一世 –

好吧,我终于想出了答案。到目前为止,你们中的很多人都希望将你的Parse服务器迁移到像Heroku这样的服务器上,因为它们将在近一个月内关闭。对于那些面临未来相同挑战并已迁移到Heroku的人,请记住,您的数据以json格式存储在mlab上。如果你想保存,查看或更新你的数据,并且你想通过http或https使用节点来访问这些数据,你将不得不使用mongodb的API(而不是PARSE SERVER API)并将其包含在packages.json中。查看mlab的documentation,而不是将您的Parse Cloud代码连接到node.js.不幸的是,Parse没有提供足够的文档来展示如何正确地使用节点和API,所以坚持使用mlab的API。