如何访问@ odata.count以在Microsoft graph-api中显示消息数

时间:2019-03-03 16:21:34

标签: node.js handlebars.js microsoft-graph

我正在玩Microsoft Graph API,并在他们的网站上关注如何制作node.js应用以显示您的inbox messages的教程。我想添加一种显示您将收到的电子邮件数量的方法。在下面运行此GET请求之后,我可以在odata.count字段中看到电子邮件数量。我希望能够在我的web app上显示它。有人可以帮忙吗?我将在下面发布代码。

Tutorial

https://docs.microsoft.com/en-us/outlook/rest/node-tutorial

GET Request to display count of emails

https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages?$count=true

mail.js

var express = require('express');
var router = express.Router();
var authHelper = require('../helpers/auth');
var graph = require('@microsoft/microsoft-graph-client');

/* GET /mail */
router.get('/', async function (req, res, next) {
  let parms = {
    title: 'Inbox',
    active: {
      inbox: true
    }
  };

  const accessToken = await authHelper.getAccessToken(req.cookies, res);
  const userName = req.cookies.graph_user_name;

  if (accessToken && userName) {
    parms.user = userName;

    // Initialize Graph client
    const client = graph.Client.init({
      authProvider: (done) => {
        done(null, accessToken);
      }
    });

    try {
      // Get the 10 newest messages from inbox
      const result = await client
        .api('/me/mailfolders/inbox/messages?$count=true')
        //api("/me/mailfolders/inbox/messages?$filter=from/emailaddress/address eq '@student.example.com'")
        //api("/me/messages?$filter=from/emailaddress/address eq '@npm.js.com'")
        .top(0)
        .select('subject,from,receivedDateTime,isRead,sentDateTime')
        // .orderby('receivedDateTime DESC')
        .count(true)
        .get();

      parms.messages = result.value;
      res.render('mail', parms);
    } catch (err) {
      parms.message = 'Error retrieving messages';
      parms.error = {
        status: `${err.code}: ${err.message}`
      };
      parms.debug = JSON.stringify(err.body, null, 2);
      res.render('error', parms);
    }

  } else {
    // Redirect to home
    res.redirect('/');
  }
});

module.exports = router;

mail.hbs

<h2> Report</h2>
<table class="table">
  <thead class="thead-light">
    <th scope="col">From</th>
    <th scope="col">Subject</th>
    <th scope="col">Received</th>
    <th scope="col">isRead</th>
    <th scope="col">Sent Time</th>
    <th scope="col">Count</th>


  </thead>
  <tbody>
    {{#each messages}}
    <tr class="{{#unless this.isRead}}table-primary{{/unless}}">
      <td title="{{this.from.emailAddress.address}}">{{this.from.emailAddress.name}}</td>
      <td>{{this.subject}}</td>
      <td>{{this.receivedDateTime}}</td>
      <td>{{this.isRead}}</td>
      <td>{{this.sentDateTime}}</td>

      {{messages.array.length}}





    </tr>
    {{/each}}
  </tbody>
</table>

0 个答案:

没有答案