Cloud Code Parse.User.current()返回null

时间:2014-08-29 13:28:07

标签: parse-platform

当我在Cloud Code Parse.User.current()中使用此功能时,返回null 我使用parseExpressCookieSession进行登录 有什么建议?

var express = require('express');
var expressLayouts = require('cloud/express-layouts');
var parseExpressHttpsRedirect = require('parse-express-https-redirect');
var parseExpressCookieSession = require('parse-express-cookie-session');

// Required for initializing enter code hereExpress app in Cloud Code.
var app = express();

// Global app configuration section
app.set('views', 'cloud/views');
app.set('view engine', 'ejs'); // Switch to Jade by replacing ejs with jade here.
app.use(expressLayouts);          // Use the layout engine for express
app.set('layout', 'layout');
app.use(parseExpressHttpsRedirect());  // Require user to be on HTTPS.
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('helloworld'));
app.use(parseExpressCookieSession({
  fetchUser: true,
  cookie: { maxAge: 3600000 * 24 }
}));    

Parse.Cloud.beforeSave('Menu', function(request, response) {
    var Business = Parse.Object.extend('Business');
    var query = new Parse.Query(Business);

    query.equalTo('profile', Parse.User.current().get('profile'));
    query.find({
        success: function(business) {
            console.log(business);
            response.success();
        },
        error: function(error) {
            response.error(error.message);
        }
    });
  });

  app.listen();

这是我用来登录/注销的代码

app.post('/login', function(req, res) {
  Parse.User.logIn(req.body.username, req.body.password).then(function(user) {
    // Login succeeded, redirect to homepage.
    // parseExpressCookieSession will automatically set cookie.
    res.redirect('/');
  },
  function(error) {
    // Login failed, redirect back to login form.
    res.redirect('/');
  });
});

// Logs out the user
app.post('/logout', function(req, res) {
  Parse.User.logOut();
  res.redirect('/');
});

1 个答案:

答案 0 :(得分:8)

这是一个老问题,但回答以后的参考。

Parse.User.current()在客户端ex中使用时可在Javascript SDK中使用。用户登录的WebApp,您可以使用该功能获取当前用户。

让用户调用 Cloud Code 功能或对对象执行操作( beforeSave afterSave beforeDelete < / strong>依此类推)你使用 request.user 属性,它包含向Parse.com发出请求的用户。

有关Parse.Cloud.FunctionRequest的更多详细信息,请访问:https://parse.com/docs/js/api/classes/Parse.Cloud.FunctionRequest.html

示例代码:

Parse.Cloud.beforeSave('Menu', function(request, response) {
   var requestUser = request.user; 
   // instance of Parse.User object of the user calling .save() on an object of class "Menu"

   // code cut for brevity
});