How to get Database data in asyncData by vue ssr

  1. asyncData runs on the server, cannot be requested with ajax, is already running on the server, and does not require ajax.
  2. you cannot require mongo a database in entry-server.js,
  3. where should I execute the query command of mongodb and send the data to asyncData?
  4. is it possible to put data into context in front of the render page?
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;
    }
    
}
Mar.29,2022

make requests in asyncData, but not with ajax, but with http in node. You can directly axios libraries, be compatible with browsers and node.js

Menu