简体   繁体   English

Parse.com Node.js通过ID更新用户

[英]Parse.com Node.js updating a user by id

I am new to Parse.com's Javascript SDK. 我是Parse.com的Javascript SDK的新手。 I'm trying to develop an admin function, where I can update a user. 我正在尝试开发一个管理功能,可以在其中更新用户。 The following function is called with by retrieving updated values of a user object in a form. 通过检索表单中用户对象的更新值来调用以下函数。

app.post('/update', function(req, res){
    var userid = req.body.userid #id retrieved from user object

    var query = new Parse.Query(Parse.User);
    query.get(userid, {
        success: function (user) {
            user.set("name",req.body.name); #name retrieved from user object
            user.save();
            res.redirect("/home");
        },
        error: function (error) {
            console.log(error);
            res.redirect("/home");
        }
    });
});

I've been trying with this codes and yet is returned with successful redirection without a clear indication of what the error is. 我一直在尝试使用此代码,但是返回成功,但没有明确指出错误是什么。

The console log for the Cloud Code server shows the following: Cloud Code服务器的控制台日志显示以下内容:

I2014-09-26T10:54:34.207Z] v001: Ran custom endpoint with: Input: {"method"=>"GET", "url"=>"/home" ...} Result: Success I2014-09-26T10:54:34.207Z] v001:使用以下命令运行自定义终结点:输入:{“ method” =>“ GET”,“ url” =>“ / home” ...}结果:成功

Any help will be appreciated! 任何帮助将不胜感激! Thanks! 谢谢!

As mentioned in the comments you will need to check what the error was when saving. 如注释中所述,您将需要检查保存时的错误。 Also remember Parse.Object.save() is an asynchronous function, you don't want to redirect until the save succeeds. 还要记住,Parse.Object.save()是一个异步函数,在保存成功之前,您不希望重定向。

app.post('/update', function(req, res) {
        var userid = req.body.userid;
        var pwd = req.body.pwd;
        Parse.User.logIn("userid", "pwd", {
            success: function (user) {
                user.set("username", "my_new_username");  // attempt to change username
                user.save(null, {
                    success: function (user) {
                        var query = new Parse.Query(Parse.User);
                        query.get(userid, {
                            success: function (user) {
                                user.set("name", req.body.name);
                                user.save(null, {
                                    success: function (savedUserObject) {
                                        res.redirect('/home');
                                    },
                                    error: function (gameScore, error) {
                                        console.log('Failed to save object: ' + error.message);
                                        res.redirect('/error')
                                    }
                                });
                                res.redirect("/home");
                            },
                            error: function (error) {
                                console.log(error);
                                res.redirect("/home");
                            }
                        });
                    }
                });
            }});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM