我正在创建一个dialogflow api,并使用MongoDb检索数据。 MOngoDB尝试连接到服务器时出现错误。
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
//const {Card, Suggestion} = require('dialogflow-fulfillment');
const mongoose = require('mongoose');
// you can use your mongodb connection url string
let uri = 'mongodb+srv://adynoruser:Adynor@123@cluster0-i0lae.gcp.mongodb.net/test?retryWrites=true';
let Song;
mongoose.connect(uri,{ useNewUrlParser: true });
let mdb = mongoose.connection;
mdb.on('error', console.error.bind(console, 'connection error:'));
mdb.once('open', function callback() {
// Create song schema
let songSchema = mongoose.Schema({
decade: String,
artist: String,
song: String,
weeksAtOne: Number
});
// Store song documents in a collection called "songs"
// this is important ie defining the model based on above schema
Song = mongoose.model('songs', songSchema);
// Create seed data
let seventies = new Song({
decade: '1970s',
artist: 'Debby Boone',
song: 'You Light Up My Life',
weeksAtOne: 10
});
//use the code below to save the above document in the database!
seventies.save(function (err) {
console.log('saved');
});
});
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
// I use the code below to find a song from database and ask the user whether he wants to listen to it
// Use the code below to extract data based on your criteria
return Song.find({ 'song': 'You Light Up My Life' }, 'song')
.then((songs) => {
//songs is araay matching criteria, see log output
console.log(songs[0].song);
agent.add(`Welcome to my agent! Would you like to listen ${songs[0].song}?`);
})
.catch((err) => {
agent.add(`Therz some problem`);
});
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
// intentMap.set('your intent name here', yourFunctionHandler);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});
我得到的错误是
连接错误:{错误:querySrv ESERVFAIL _mongodb._tcp.cluster0-i0lae.gcp.mongodb.net 在errnoException(dns.js:28:10) 在QueryReqWrap.onresolve [作为oncomplete](dns.js:219:19) 代码:“ ESERVFAIL”, errno:“ ESERVFAIL”, syscall:“ querySrv”, 主机名:'_mongodb._tcp.cluster0-i0lae.gcp.mongodb.net'}
我对Mongodb完全陌生。任何帮助表示赞赏。