使用会话在不同的文件和路由之间共享数据

时间:2019-09-23 13:11:36

标签: node.js express session dialogflow

node.js相对较新,我想知道是否有人可以帮助我。

我的MERN堆栈工作正常,并且身份验证正常。

我正在将Google的Dialogflow集成到MERN堆栈中,我想将用户的用户名传递给该fulfillmentRoutes.js文件。

fulfillmentRoutes.js如下(我删除了不必要的代码,这只是裸露的骨头);

fulFillmentRoutes.js

const {WebhookClient, Payload, Card} = require('dialogflow-fulfillment');
const express = require('express');
const router = express.Router();
const jwtDecode = require('jwt-decode');
const chatbot = require('../chatbot/chatbot');
const mongoose = require('mongoose');
const passport = require('passport');
const keys = require('../config/keys');
const Coupon = mongoose.model('coupon');
const Counter = mongoose.model('counters');
const Define = mongoose.model('define');
const sourceFile = require('./api/users.js');

const Themes = require('../models/Themes');
const Time = require('../models/Time');
const Questionexp = require('../models/Questionexp');
var cookieParser = require('cookie-parser');
var session = require('express-session');


module.exports = app => {

app.post('/api/df_text_query', async (req, res) => {

let responses = await chatbot.textQuery(req.body.text, req.body.userID, req.body.parameters);
res.send(responses[0].queryResult);

});

app.post('/api/df_event_query', async (req, res) => {
let responses = await chatbot.eventQuery(req.body.event, req.body.userID, req.body.parameters);
res.send(responses[0].queryResult);


});



app.post('/', async (req, res) => {

const agent = new WebhookClient({ request: req, response: res });

async function learn(agent) {

Demand.findOne({'course': agent.parameters.courses}, function(err, course) {
if (course !== null ) {
course.counter++;
course.save();
} else {
const demand = new Demand({course: agent.parameters.courses});
demand.save();
}
});
let responseText = `You want to learn about ${agent.parameters.courses}.
Here is a link to all of my courses: https://www.udemy.com/user/jana-bergant`;

let coupon = await Coupon.findOne({'course': agent.parameters.courses});
if (coupon !== null ) {
responseText = `You want to learn about ${agent.parameters.courses}.
Here is a link to the course: ${coupon.link}`;
}

agent.add(responseText);
}

async function welcome(agent) {

agent.add(“Hello!);

}


function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}

let intentMap = new Map();


intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Welcome Intent - custom', continuesession);

agent.handleRequest(intentMap);
});

}

文件上方的文件位于api文件夹之外的文件夹中,但在routes文件夹中。

我要共享用户名是使用上面的实现模块进行个性化对话。

我已经在主应用程序根目录中的server.js文件中声明了一个会话。

server.js

app.use(session({
         resave: true,
         saveUninitialized: true,
         secret: "secret"
        }));

在./routes/api目录中的users.js文件中,我在users.js中做了以下操作

users.js

router.get('/current', passport.authenticate('jwt', { session: false }),
  (req, res) => {
    req.session.test = req.user.email;
 res.send(req.session.test);
    res.json({
      id: req.user.id,
      firstname: req.user.firstname,
      lastname: req.user.lastname,
      email: req.user.email,
      week: req.user.week,
      age: req.user.age,
      time: req.user.time,
      bookmark: req.user.bookmark,
      alertDate: req.user.alertDate
    });
  }
);

但是,当我在fulfillmentRoutes.js(在开头引用的文件)中使用会话提取行(在下面)时,我不确定。

username = req.session.test;`

请提供任何帮助。

谢谢

P

0 个答案:

没有答案