[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
1. 出现问题
(node:2171) UnhandledPromiseRejectionWarning: TypeError: Assignment to
constant variable. at getList
(/Users/dr/Desktop/Project/nodeBlog/src/controller/blog.js:10:9) at
handleBlogRouter (/Users/dr/Desktop/Project/nodeBlog/src/router/blog.js:18:30)
at getPostData.then.postData (/Users/dr/Desktop/Project/nodeBlog/app.js:56:28)
at process._tickCallback (internal/process/next_tick.js:68:7) (node:2171)
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error
originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch().
(rejection id: 1) (node:2171) [DEP0018] DeprecationWarning: Unhandled promise
rejections are deprecated. In the future, promise rejections that are not
handled will terminate the Node.js process with a non-zero exit code.
2. 问题分析
(1)源码
const blogResult = handleBlogRouter(req, res); if(blogResult){
blogResult.then(blogData => { res.end( JSON.stringify(blogData) ) }) return }
(2) 原因
const blogResult = handleBlogRouter(req, res)
定义了变量且存在初始值,而const的值会随着传入参数的不同发生变化,所以发生了报错:TypeError: Assignment to constant
variable.
3. 解决方法
将const 改为 let
4. const与let的比较
(1)区别:
* const一般是声明常量
如const a=1,const声明的变量不得改变值,这意味着,const一旦声明变量,就必须立即初始化,不能留到以后赋值。如const
a这样会报错
* let声明的变量可以改变,值和类型都可以改变,没有限制。
* const定义变量必须赋初始值,let不需要赋初始值
(2)共同点:
* let与const都是只在声明所在的块级作用域内有效。