微信自定义菜单开发,启动服务控制台报错Cannot read property 'then' of undefined
来源:
沙漠江河
2017-01-21
replay.js
wechatApi.deleteMenu().then(function () {
return wechatApi.createMenu(menu)
})
.then(function (msg) {
console.log(msg);
})
---------------------------------------------------
wechat.js
Wechat.prototype.deleteMenu = function () {
var that = this;
return new Promise(function (resolve, reject) {
that
.fetchAccessToken()
.then(function (data) {————————————————————>报错then未定义,之前其他使用then的地方没有错误
var url = api.menu.delete + 'access_token=' + data.access_token;
request({"url": url, "json": true}).then(function (response) {
var _data = response["body"];
//console.log(response);
//return;
if (_data) {
resolve(_data);
} else {
throw new Error('deleteMenu fails');
}
}).catch(function (err) {
reject(err);
})
})
});
}
4回答
-
Scott
2017-02-02
var that = this;
this.getAccessToken()
这里加 return
return this.getAccessToken()
00 -
Scott
2017-01-23
fetchAccessToken 以及构造函数 Wechat 里面代码也贴一下,看看是不是没有正确的返回的 promise,少了一个 return
00 -
沙漠江河
提问者
2017-02-03
好的知道了,问题解决了。谢谢老师
00 -
沙漠江河
提问者
2017-01-31
Scott老师你好!你是说哪里少了return,以下代码是fetchAccessToken 以及构造函数 Wechat;
function Wechat(opts) {
var that = this;
this.appID = opts.appID;
this.appSecret = opts.appSecret;
this.getAccessToken = opts.getAccessToken;
this.saveAccessToken = opts.saveAccessToken;
this.getTicket = opts.getTicket;
this.saveTicket = opts.saveTicket;
this.fetchAccessToken();
}
Wechat.prototype.fetchAccessToken = function(data) {
//debugger;
var that = this;
this.getAccessToken()
.then(function(data) {
try {
data = JSON.parse(data);
} catch (e) {
return that.updateAccessToken();
}
if (that.isValidAccessToken(data)) {
return Promise.resolve(data);
} else {
return that.updateAccessToken();
}
})
.then(function(data) {
//that.access_token = data.access_token;
//that.expires_in = data.expires_in;
that.saveAccessToken(data);
return Promise.resolve(data);
})
}
00
相似问题