方法在客户端返回未定义

时间:2019-02-19 06:39:16

标签: mongodb meteor

我能够在服务器上生成随机随机数,它会显示在控制台上,但是该值不会返回给客户端。

服务器方法:

Meteor.methods({
    'user.generateNewLoginAttempt': function(user_address) {
        let exists = Meteor.users.findOne({
            username: user_address
        });
        let nonce = Random.secret(16);

        console.log(nonce); //shows random string

        if (exists) {
            // user already exists, update nonce
            Meteor.users.update(exists._id, {
                $set: {
                    nonce: nonce
                }
            });
        } else {
            // create new user
            let userId = Accounts.createUser({
                username: user_address,
                address: user_address,
                nonce: nonce
            });
        }

        return nonce;
    },
});

客户代码:

Meteor.call('user.generateNewLoginAttempt', user_address, function(err, nonce) {
    if (err)
        console.log(err);
    /shows up internal server error
    // nonce = the nonce generated on server
    else {
        console.log('nonce: ' + nonce);
        // hash nonce
        let sh3_nonce = web3.sha3(nonce); // depending on web3 version use: web3.utils.sha3(nonce)
        console.log('sh3 hashed nonce: ' + sh3_nonce);

        // sign hashed nonce
        web3.eth.sign(user_address, sh3_nonce, function(err, res) {
            if (err) {
                console.log(err);
            } else {
                // res = the signed nonce
                // login with signed nonce
                login(res);
            }
        });
    }
});

在客户端,它显示Internal Server Error(500)。我尝试从服务器方法中删除大多数代码,只是返回一些值,但这还是行不通的。

1 个答案:

答案 0 :(得分:1)

我认为您不能像这样设置用户文档中的现时和地址,因为我记得Accounts.createUser仅使用用户名,密码,电子邮件和配置文件,因此如果要保存现时和地址在用户对象上,您需要将它们插入“个人资料”对象中。

保持头脑!不鼓励使用配置文件保留用户数据,因为默认的流星行为是允许用户编辑配置文件,因此,如果这样做,则需要确保设置自己允许/拒绝策略。

我认为将用户详细信息保存在单独的集合中会好很多。