Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用户注册时赋值其他属性报错206 #32

Open
stt106 opened this issue May 30, 2021 · 5 comments
Open

用户注册时赋值其他属性报错206 #32

stt106 opened this issue May 30, 2021 · 5 comments

Comments

@stt106
Copy link

stt106 commented May 30, 2021

我在官方文档上看到可以这样注册用户并且赋值用户的其他属性:

func (lc LeanCloudDB) CreatePlayer(name string, password string, points int) (string, error) {
	player, err := lc.client.Users.SignUp(name, password)
	if err != nil {
		panic(err)
	}

	// TODO: why does this NOT work?
	err = lc.client.Users.ID(player.ID).Set("points", points)
	 if err != nil {
	 	panic(err)
	}
	return player.ID, nil
}

但是我这样用却不行: panic: 206 The user cannot be altered by a client without the session.
请问我什么地方用的不对?我是在一个console app里测试时出现这个问题的。用户注册成功了。

@soasurs
Copy link
Contributor

soasurs commented May 31, 2021

Go SDK 的设计是针对每个请求做鉴权,而不是在 client 上附加全局的鉴权。

参考 #26 (comment)

@stt106
Copy link
Author

stt106 commented May 31, 2021

我看了26里面的评论,改了一下代码,现在没有报错了,但是赋值没成功,_User里面没有我赋值的属性(points)。

func (lc LeanCloudDB) CreatePlayer(name string, password string, points int) (string, error) {
	player, err := lc.client.Users.SignUp(name, password)
	if err != nil {
		panic(err)
	}

	// TODO: why does this NOT work?
	err = lc.client.User(player).Set("points", points) // lc.client.Users.ID(player.ID).Set("points", points)
	if err != nil {
		panic(err)
	}
	return player.ID, nil
}

@soasurs
Copy link
Contributor

soasurs commented May 31, 2021

Set 方法的最后一个参数为可变参数,主要用于使用特定的鉴权方式。如果想在当前请求中使用 某个特定用户,请使用 UseUser(user)UseSessionToken(token)

例如:

func (lc LeanCloudDB) CreatePlayer(name string, password string, points int) (string, error) {
	player, err := lc.client.Users.SignUp(name, password)
	if err != nil {
		panic(err)
	}

	err = lc.client.User(player).Set("points", points, UseUser(player))
	if err != nil {
		panic(err)
	}
	return player.ID, nil
}

@stt106
Copy link
Author

stt106 commented May 31, 2021

感谢回复,但是不知道为啥,我按照你说的用法,还是无法赋值points属性,新用户还是可以正常注册,但是User里还是没有points的属性。
image

@stt106
Copy link
Author

stt106 commented May 31, 2021

我的LeanCloudDB 只是一个简单的结构体封装来实现数据库的接口。
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants