我使用的是统一的GameSparks,我试图从数据库中提取某些数据。
我设置了一个名为"getItem“的事件,其属性"type”设置为“in”。
我设置了一个云代码事件,通过使用"type“属性来访问该事件,该属性实际上将访问数据中的description字段。
var description = Spark.getData().type; // get the type we passed in
if(description !== ""){
// if the type wasnt an empty string, then we can use the type in our query
Spark.setScriptData('items', Spark.metaCollection('items').find({"description": description}));
}在Test中,我对Log进行身份验证,然后使用这个JSON执行日志事件
{
"@class": ".LogEventRequest",
"eventKey": "getItem",
"type": "Sharp"
}在检查器中,我看到语句计数:2,请求和响应为
{
"@class": ".LogEventResponse",
"scriptData": {
"items": [
{
"_id": {
"$oid": "59160a27feeace0001d90f7f"
},
"shortCode": "sword",
"name": "Stone Sword",
"description": "Sharp",
}
]
}
} 在我的统一代码中,我已经设置了所有内容,我进行了身份验证,并在按钮上单击它调用如下:
new GameSparks.Api.Requests.LogEventRequest()
.SetEventKey("getItem")
.SetEventAttribute("type", "Sharp")
.Send((response) => {
if (!response.HasErrors) {
GSData data = response.ScriptData.GetGSData("items");
print("Item ID: " + data.GetString("name"));
} else {
Debug.Log("Error Saving Player Data...");
}
});这时,我得到了“对象引用未设置为对象实例”的流。
如果删除print语句,它不会抛出错误。它似乎没有找到任何描述锐利,即使测试线束。
我尝试过许多代码的变体,但无法使它工作。
发布于 2017-06-22 18:23:27
正如您正确地发现的,错误来自您响应中的数据与用于检索它的getter之间的不匹配。
因为“items”字段包含需要使用的数组
列表数据=response.ScriptData.GetGSDataList(“项”);
然后遍历列表。
若要返回单个对象而不是数组,可以将云代码查询更改为:
Spark.metaCollection('items').findOne({"description":描述})
注意,如果多个文档满足所提供的查询,mongo将返回它找到的第一个文档。
如果您在使用GameSparks时有任何进一步的问题或遇到任何问题,可以通过- https://support.gamesparks.net/support/home与我们的支持小组联系。
https://stackoverflow.com/questions/43946198
复制相似问题