search

Home  >  Q&A  >  body text

node.js - 想问下sequelize怎么同步查询啊?因为要查比较多的内容

天蓬老师天蓬老师2872 days ago669

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 15:54:51

    • findeOne returns a promise. Does your code above work? ? ? findeOne返回的是一个promise, 你上面的代码执行有用???

    const [sysnotice, sysactivity] = await Promise.all([db.sys_news.findOne(),  db.sysactivity.findOne()]);
    res.render("index", {
       sysnotice: sysnotice,
        sysactivity:sysactivity
    });
    • 如果不用await

    • Promise.all([db.sys_news.findOne(),  db.sysactivity.findOne()]).then(result => {
          const [sysnotice, sysactivity] = result;
          res.render("index", {
            sysnotice: sysnotice,
            sysactivity:sysactivity
          });  
      }).catch(error => console.error(error));
    • If you don’t use await, use the following code

    • rrreee
    🎜🎜If there are a lot of arrays, use other methods to return data instead of manually entering the array one by one🎜🎜🎜

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 15:54:51

    You can use bluebird’s map method

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:54:51

    ES6

    Using the co moduleco模块

    const co = require('co');
    
    co(function *(){
        let sysnotice = yield db.sys_news.findOne();
        let sysactivity = yield db.sysactivity.findOne();
        res.render("index", {
            sysnotice: sysnotice,
            sysactivity:sysactivity
        });
    })

    ES5
    采用bluebird

    var Promise = require('bluebird');
    
    Promise.all([db.sys_news.findOne(),  db.sysactivity.findOne()]).then(function(results){
        res.render("index", {
            sysnotice: results[0],
            sysactivity:results[1]
        });
    })

    ES5
    adopts the bluebird module🎜 rrreee

    reply
    0
  • Cancelreply