微信小程序云开发报错:ReferenceError: phone is not defined
此页面 功能实现目标为点击按钮后通过getPhoneNumber获取用户手机号,并查找是否已存在于数据库中,若存在则存储信息并跳转至另一页面,现在在数据库查找这一步卡住了,求帮助。
pages\index\index.js 代码
Page({
data: {
logintext: '登陆',
userInfo: {},
phone: {},
......
},
.......
getPhoneNumber(e) {
var that = this;
wx.showLoading({
title: '加载中,请稍后...',
}),wx.login({
success(res){
wx.cloud.callFunction({
name: 'get-phone-number',
data: {
cloudID: e.detail.cloudID
}
}).then(res => {
this.setData({phone:res.result.list[0].data.phoneNumber})
})
},
})
db.collection("userlist").doc(phone).get.then(res=>{
console.log(res)
})
}
// 云函数入口文件
const cloud = require('wx-server-sdk')
//const requestpromise = require('request-promise');
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
// 云函数入口函数
exports.main = async (event, context) => {
console.log("上传的cloudID", event.cloudID)
return await cloud.getOpenData({
list: [event.cloudID]
})
}
根据用户手机号查询的代码有问题,改成下面这样,错误看注释
getPhoneNumber(e) {
var that = this;
wx.showLoading({
title: '加载中,请稍后...',
}),
wx.login({
success(res) {
wx.cloud.callFunction({
name: 'get-phone-number',
data: {
cloudID: e.detail.cloudID
}
}).then(res => {
this.setData({ phone: res.result.list[0].data.phoneNumber })
//放回调里面执行查询
var phone = res.result.list[0].data.phoneNumber;//////定义变量并且赋值
db.collection("userlist").doc(phone).get.then(res => {
console.log(res)
})
})
},
})
//这里的phone变量没定义,而且wx.login异步的,就算定义了phone也得不到值,要放到then回调里面执行下面的代码或者改成async+await模式
/*
db.collection("userlist").doc(phone).get.then(res => {
console.log(res)
})*/
}
phone作为参数名,应该用双引号包裹。
或者已经有定义这个phone变量。
报错意思是phone变量未定义。
this.setData({"phone":res.result.list[0].data.phoneNumber})
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!