williswin
williswin

Reputation: 133

How to read from multiple CosmosDB documents

I am stuck trying to figure out how to get my list to read from a specific query. If I comment out one of the app.get it reads from one DocumentDB and vice versa. How do I get it to read both my queries at the same time or specify one list to read from one query and another to read from a second query? I attempted a second querySpec, not sure if that is the way to go though.

Thank you

I was able to get assistance from my previous question. Connecting to multiple CosmosDB documents

Original tutorial: https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-nodejs-application#_Toc395783181

Code in tasklist.js

const TaskDao = require("../models/taskDao");

 class TaskList {
   /**
    * Handles the various APIs for displaying and managing tasks
    * @param {TaskDao} taskDao
    */
   constructor(taskDao,taskDao2) {
     this.taskDao = taskDao;
     this.taskDao2 = taskDao2;
   }

   async showList1(req, res) {
    const querySpec = {
      query: "SELECT * FROM root r WHERE r.List1=@List1",
      parameters: [
        {
          name: "@List1",
          value: "yes"
        }
      ]

    };

     const items = await this.taskDao2.find(querySpec);
     res.render("index", {
       title: "Form",
       tasks: items
     });

   }


   async showList2(req, res) {
    const querySpec2 = {
      query: "SELECT * FROM root r WHERE r.List2=@List2",
       parameters: [
         {
           name: "@List2",
           value: true
        }
      ]
    };

     const items = await this.taskDao.find(querySpec2);
     res.render("index", {
       title: "Form",
       tasks: items
     });

   }

   async addTask(req, res) {
     const item = req.body;

     await this.taskDao.addItem(item);
     res.redirect("/");
   }
 }

 module.exports = TaskList;

Code in app.js

app.get('/', (req, res, next) => taskList.showList1(req, res).catch(next))
app.get('/', (req, res, next) => taskList.showList2(req, res).catch(next))

I would like for these lists to read different values.

enter image description here

Upvotes: 1

Views: 374

Answers (1)

Stanley Gong
Stanley Gong

Reputation: 12153

If you just want to load data from two different containers to your HTMLselector, you should use res.send to send your data instead of res.render.

Try the steps below : 1. Go to app.js add two route codes here :

 app.get('/list1', (req, res, next) => taskList.showTasks1(req, res).catch(next))
 app.get('/list2', (req, res, next) => taskList.showTasks2(req, res).catch(next)) 

enter image description here

2.Go to routes/tasklist.js, add showTasks1 and showTasks2(this code is based on my previous answer taskDao2 is needed here.) :

   async showTasks1(req, res) {
    const items = await this.taskDao.find("SELECT * FROM root r ");
    res.send(items);
  }
  async showTasks2(req, res) {
    const items = await this.taskDao2.find("SELECT * FROM root r ");
    res.send(items);
  }

enter image description here

Run this project, you can get data from your two containers by URL :

http://localhost:3000/list1 and http://localhost:3000/list2

enter image description here

Finally, just refer to my first answer for you to load data to selector.

Hope it helps. If you have any further concerns, pls feel free to let me know.

Upvotes: 2

Related Questions