亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

eggjs 在定時器訪問service提示Cannot read property

eggjs 在定時器訪問service提示Cannot read property

精慕HU 2019-02-27 12:27:58
update_article.js(schedule目錄下):const Subscription = require('egg').Subscription;class UpdateArticleStatus extends Subscription {    // 通過 schedule 屬性來設置定時任務的執行間隔等配置    static get schedule() {        return {            interval: '2s', // 1 分鐘間隔            type: 'all', // 指定所有的 worker 都需要執行        };    }    * subscribe(ctx) {        const res = yield ctx.service.article.getAllArticles(1);        console.log(res);    }}module.exports = UpdateArticleStatus;運行提示:excute error. Cannot read property 'service' of undefined我照官網的例子:module.exports = {  schedule: {    interval: '1m', // 1 分鐘間隔    type: 'all', // 指定所有的 worker 都需要執行  },  * task(ctx) {    const res = yield ctx.curl('http://www.api.com/cache', {      dataType: 'json',    });    ctx.app.cache = res.data;  },};發現curl屬性也是不能訪問
查看完整描述

2 回答

?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

它已經提示你了, Cannot read property 'service' of undefined
說明 service 屬性前面的 ctx 是 undefined。

你這屬于混著寫,不遵守他示例的格式。

第一個寫法里面,subscribe 函數是沒有 ctx 這個參數的。

第二個屬于簡寫寫法,ctx.curl 是可以訪問的啊,你可以在 task 函數里直接

console.log(ctx.curl.toString());

是可以訪問到函數原型的,沒有明白你這個 curl 無法訪問的意思。


查看完整回答
反對 回復 2019-03-06
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

缺了this

用this.ctx

而且要記得在頭部引用 const Subscription = require('egg').Subscription;

另外egg2.0已經支持await如果升級到2.0,可以用await


查看完整回答
反對 回復 2019-03-06
  • 2 回答
  • 0 關注
  • 1969 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號