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

Saga 处理异步请求首次登陆的问题 #5

Open
tilkofjin opened this issue Aug 25, 2020 · 0 comments
Open

Saga 处理异步请求首次登陆的问题 #5

tilkofjin opened this issue Aug 25, 2020 · 0 comments

Comments

@tilkofjin
Copy link

首先感谢作者的开源教程,但在学习 1.5. 实战 Redux 异步工作流时遇到个小问题,简单描述下:本地写好saga相关逻辑,云函数准备就绪,云数据库 user 表连接成功,且未添加任何默认用户,执行首次登录,云函数返回的 user为以下内容:
{
"result": {
"user": {
"_document": {
"_db": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
},
"Geo": {},
"command": {
"aggregate": {},
"project": {}
}
},
"_coll": "user",
"id": "ac5f38825f44cc08004b74f216aa9cc5",
"request": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
}
},
"_apiOptions": {}
},
"collection": {
"_query": {
"_db": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
},
"Geo": {},
"command": {
"aggregate": {},
"project": {}
}
},
"_coll": "user",
"_fieldFilters": "",
"_apiOptions": {},
"_request": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
}
}
},
"collectionName": "user",
"database": {
"cloud": {
"inited": true,
"services": {
"database": {
"name": "database"
},
"storage": {
"name": "storage"
},
"functions": {
"name": "functions"
},
"open": {
"name": "open"
},
"utils": {
"name": "utils",
"initRequired": false
},
"openapi": {
"name": "openapi"
},
"cloudPay": {
"name": "cloudPay"
}
},
"debug": false,
"instanceForEnv": {},
"exportAPI": {
"version": "2.2.0",
"cloudPay": {}
},
"meta": {
"session_id": "1598341789643"
},
"config": {
"env": {}
},
"provider": {
"api": {}
},
"instance": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
},
"extensionMap": {}
}
},
"config": {},
"_db": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
},
"Geo": {},
"command": {
"aggregate": {},
"project": {}
}
},
"command": {
"aggregate": {},
"project": {}
},
"Geo": {}
},
"_collection": {
"_db": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
},
"Geo": {},
"command": {
"aggregate": {},
"project": {}
}
},
"_coll": "user",
"_fieldFilters": "",
"_apiOptions": {},
"_request": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
}
}
}
},
"database": {
"cloud": {
"inited": true,
"services": {
"database": {
"name": "database"
},
"storage": {
"name": "storage"
},
"functions": {
"name": "functions"
},
"open": {
"name": "open"
},
"utils": {
"name": "utils",
"initRequired": false
},
"openapi": {
"name": "openapi"
},
"cloudPay": {
"name": "cloudPay"
}
},
"debug": false,
"instanceForEnv": {},
"exportAPI": {
"version": "2.2.0",
"cloudPay": {}
},
"meta": {
"session_id": "1598341789643"
},
"config": {
"env": {}
},
"provider": {
"api": {}
},
"instance": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
},
"extensionMap": {}
}
},
"config": {},
"_db": {
"config": {
"debug": false,
"isHttp": false,
"headers": {},
"timeout": 15000,
"version": "wx-server-sdk/2.2.0",
"throwOnCode": false
},
"Geo": {},
"command": {
"aggregate": {},
"project": {}
}
},
"command": {
"aggregate": {},
"project": {}
},
"Geo": {}
},
"_id": "ac5f38825f44cc08004b74f216aa9cc5"
}
},
"requestID": "2a8aeb0f-e6ad-11ea-a341-52540022b804"
}


查看云数据库 user 表中,发现用户信息添加成功,由于请求是异步的,本地的 storage 会将上面的错误内容缓存到本地,导致页面首次登陆时渲染非预期效果,建议将functions/login/index.js的云函数修改成 Promise 风格调用,具体只涉及到一句代码:
原代码:const user = await db.collection('user').doc(_id)
修改后:const user = await db.collection('user').doc(_id).get().then(res=>{return res.data})

再次感谢作者开源!优秀的开源教程,有机会录成视频会更有利于传播。

@tilkofjin tilkofjin changed the title Saga 处理异步请求首次登陆时的问题 Saga 处理异步请求首次登陆的问题 Aug 25, 2020
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

1 participant