账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue ssr如何在asyncData中获取数据库数据
    27
    0
    1. asyncData 运行在服务器中,不可以用ajax请求,本来就在服务器中运行,也不需要ajax。
    2. 在entry-server.js中也不能require mongo数据库,
    3. 那应该在哪来执行mongodb的查询命令,把数据给asyncData中?
    4. 难道在渲染页面前,把数据放到context中?
    const baseRender = async function(ctx, next){
        let context = {url: ctx.request.url};
        let htmlStr = await new Promise((resolve, reject) => {
            renderer.renderToString(context, (err, html) => {
          if (err) {
            reject(err)
          }
          resolve(html);
        });
        }).catch(e => {
            console.error(e);
            return e;
        });
        if (htmlStr.code && htmlStr.code === 404) {
            await next();
        } else {
            ctx.body = htmlStr;
        }
        
    }
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 俞子期 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部