微信小程序学习笔记(4) 第三方库request promise使用

1、在github官网上搜索request:

 2、找到request-promise标签处:

 3、开发工具中右键云函数名称,点击“在外部终端创建中打开”启动终端。

4、使用npm命令依次安装request和request promise。

 5、成功安装后程序目录会出现node_modules文件夹及。

 

  6、在云函数index.js文件引用request-promise。

//引入 request-promise
var rp = require('request-promise');

7、在main函数return中调用外部接口:

// 云函数入口函数
exports.main = async (event, context) => {

return rp('https://api.tianapi.com/film/index?key=06d58e42fdf38ecbf343b167612001eb&num=10')
    .then(function (res) {
        console.log(res);
        return res;
    })
    .catch(function (err) {
        console.log(err);
    });
}

8、在小程序中调用云函数。

 /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.cloud.callFunction({
             "name": 'movielist'
        }).then(res => {
            this.setData({
                movieList: JSON.parse(res.result).newslist
            })
            console.log(res);
        }).catch(err => {
            console.error(err);
        })
    },

BTW,在云开发控制台中可以管理及测试云函数。

 

posted @ 2021-09-26 11:23  无敌师爷IT技术Blog  阅读(960)  评论(0编辑  收藏  举报