最近在学习微信云开发,不得不感叹云开发真的厉害哈,对于前端开发来说,在这种serverless模式下,很多需要后台的工作,只需要查看官方文档调用各种接口,数据就可以信手拈来~
但是我看到云开发里的存储可以建立一个个文件夹的时候
如果想要在云函数里查看文件夹里的图片的时候,一个个id地调用出来太长了,太麻烦了

这里的微信开发文档里面并没有说接口去查看文件夹里的信息https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html
然后就出现了这一幕https://developers.weixin.qq.com/community/develop/doc/000ce2df44cf48d9f46bef1a051800
接着热心的赵大大告诉我要去Node.js 管理端 SDK API 参考 这里去看
啊原来还分小程序API和服务端API啊


好家伙,一打开这里,萌新的我简直发现新世界。但是实际告诉这里一个高级的副本,想要刷这个副本,你得对node.js和npm有了解才行
话说回来,我就不信我还不会用这个接口(菜鸡理直气壮)。
没错就是它了,列出文件夹里的所有文件!listDirectoryFiles

看着文档代码我咔咔一顿复制粘贴

结果,控制台各种泛红。


这时候赵大大发来了热(chao)心(feng)问候:

。
垂头丧气的我,仔细查阅文档。
第一步:先在需要用的云函数上安装 npm install --save wx-server-sdk@latest

第二步:执行 npm install

第三步:引入
这里建议尽量使用第一个了,因为用import语法的话,可能会报错。因为好像是Node.js运行时不支持的任何JavaScript语法都会引发UserCodeSyntaxError: SyntaxError: Unexpected identifier这个问题。

第四步:快乐coding并查看数据

最后拿到数据就可以根据result里面的路径为所欲为了哈哈
下面有注意的几个点:
1.SDK初始化的时候,如果是在云函数里面使用的,可以免密钥初始化(这里可是吃一堑长一智

)
毕竟本萌新可是赵师傅热心的指导下走出新手村的
赵老师:


2.代码不能直抄,一不小心就会出现这种报错Class constructor CloudBase cannot be invoked without ‘new’
出现这种问题,多半是老师的结构跟我的不一样,这种高端的语法怎么适合我这种萌新呢,还是老老实实,初始化写在外面再+一个export main吧

3.一定要仔细查看文档。本萌新血泪教训,急于求成只会弄巧成拙。例如:
赵老师:

赵老师:

最后的最后,感谢赵大大赵老师~~~~!在您的英明指引下,本萌新成功使用到了高端的接口~~
虽然您的反复劝退
赵老师:

赵老师:
赵老师:
但是本萌新不会放弃的!撒花,谢谢