I'm currently using botkit-sms to send and receive texts, integrated with mongoose for storing my data. I want to get a user based on their phone number, so in my code I use a controller that is supposed to find the user by their phone number in the Mongoose User Schema. However, when getUserType is called, phoneNumber
is undefined. Does anyone know how to fix this? I feel that it may have to do with Javascript's asynchronous nature, but am still getting comfortable with the language so am not completely sure.
controller.hears(['HELPME'], 'message_received', (bot, message) => {
bot.startConversation(message, (err, convo) => {
var phoneNumber = message.user
console.log('phone number defined here' + phoneNumber)
Users.getUserType((err, user, phoneNumber) => {
console.log('not getting a user back here' + user.type)
})
})
})
And then in my user_controller I just have:
export const getUserType = (callback, phoneNumber) => {
console.log('phone number is undefined here' + phoneNumber);
User.findOne({ 'phoneNumber': phoneNumber }).exec(callback);
}
I'd appreciate any help! Thanks.
You are only passing in the callback function to getUserType
, but getUserType
accepts two arguments so,
change this:
Users.getUserType((err, user, phoneNumber) => {
console.log('not getting a user back here' + user.type)
})
to
Users.getUserType((err, user, phoneNumber) => {
console.log('not getting a user back here' + user.type)
}, phoneNumber)