uni-app SSR使用JQL或者unicloud-db触发schema扩展时传的userInfo和clientInfo都是空的?
uni-app SSR使用JQL或者unicloud-db触发schema扩展时传的userInfo和clientInfo都是空的?
信息类别 | 内容 |
---|---|
产品分类 | uniCloud/App |
示例代码:
let testData = ssrRef({}, 'testData')
onServerPrefetch(async (e) => {
let res = await uniCloud.database().collection('uni-cms-articles').where('_id=="67515cf521821bdf93ae4056"').get()
testData.value = res.result.data
})
// userInfo和clientInfo没有任何有效信息
afterRead: async function ({userInfo, clientInfo, result, where, field}) {
操作步骤:
- 使用unicloud-db或者JQL通过SSR获取数据
预期结果:
- ssr触发schema扩展时,传给schema扩展的userInfo和clientInfo需要正常包含客户端信息
实际结果:
- userInfo和clientInfo基本都是空的
bug描述:
通过SSR和unicloud-db或者JQL获取的数据触发*.schema.ext.js时传的userInfo和clientInfo不包含客户端信息!! 比如uni-cms-articles.schema.ext.js。连token和uid都拿不到,这就没法做用户相关的数据处理了。
这个是哪里需要配置么?!?!?
如果是我自己的云函数调用云函数我还能通过传参自己传。 但是ssr触发的schema扩展我没法自己控制传参啊
uni-cms 配置文件配置clientAppIds了吗?
配置了,我客户端直接获取数据是有走schema扩展拿到处理后的数据的
我重新上传下config再试试,今天的数据库免费读取次数用完了。。
找到原因了,是因为ssr云函数调用schema扩展时参数clientInfo中没有appId,求问大佬这个有的解么? // 读取文章后触发 afterRead: async function ({userInfo, clientInfo, result, where, field}) { clientInfo.appId是undefined
回复 红色熊猫: 看了下服务器日志,ssr云服务调用schema扩展传入的clientInfo中的数据很少: “clientInfo”: { “DEVICEID”: “17334726045083952035”, “deviceId”: “17334726045083952035”, “locale”: “en”, “LOCALE”: “en” }
ssr云函数调用schema扩展时参数clientInfo中没有appId
求解
针对你提到的在uni-app SSR(服务器端渲染)使用JQL或unicloud-db触发schema扩展时,userInfo
和clientInfo
为空的问题,这通常是由于SSR的上下文在服务器端与客户端之间传递数据时未能正确携带这些信息。在SSR场景下,服务器在渲染页面时,需要模拟客户端请求,并传递必要的用户信息。
以下是一个示例,展示如何在uni-app SSR中通过自定义中间件或钩子函数来传递userInfo
和clientInfo
:
-
定义中间件或钩子函数:
首先,在你的SSR项目中,找到一个合适的位置(如
server.js
或app.js
)来定义中间件或钩子函数,用于处理每个请求并设置必要的上下文信息。const express = require('express'); const { createSSRApp } = require('[@dcloudio](/user/dcloudio)/uni-app'); const { renderToString } = require('[@vue](/user/vue)/server-renderer'); const app = express(); app.use(async (req, res, next) => { // 假设你已经有方法获取userInfo和clientInfo const userInfo = getUserInfoFromRequest(req); // 实现你的逻辑来获取用户信息 const clientInfo = getClientInfoFromRequest(req); // 实现你的逻辑来获取客户端信息 // 将信息附加到req对象上,供后续处理 req.userInfo = userInfo; req.clientInfo = clientInfo; next(); }); app.get('*', async (req, res) => { const ssrApp = createSSRApp(req.path); // 在这里你可以将req.userInfo和req.clientInfo传递给SSR应用 ssrApp.context.userInfo = req.userInfo; ssrApp.context.clientInfo = req.clientInfo; const appContent = await renderToString(ssrApp); res.send(appContent); }); app.listen(3000, () => { console.log('Server is running on http://localhost:3000'); });
-
在Schema扩展中使用:
在你的schema扩展逻辑中,你可以从SSR应用的上下文中访问
userInfo
和clientInfo
。db.collection('your-collection').where({ // 你的查询条件 }).get().then(res => { // 假设你在这里处理schema扩展 const userInfo = cloud.getWXContext().OPENID; // 示例,实际应使用SSR传递的userInfo const clientInfo = cloud.getWXContext().APPID; // 示例,实际应使用SSR传递的clientInfo // 你的业务逻辑,如根据userInfo和clientInfo修改查询结果或进行其他操作 });
注意:上述代码是一个简化的示例,实际项目中需要根据具体框架和配置进行调整。特别是获取userInfo
和clientInfo
的方法,可能需要根据你的认证和会话管理机制来实现。确保在SSR的服务器端渲染流程中正确传递和访问这些上下文信息。