簡體   English   中英

無法在Botpress(axios)上使用外部API

[英]Unable to use external API on Botpress (axios)

嘗試使用軸查詢外部Weather API時,出現此錯誤

ReferenceError: axios is not defined
at getTropicalCyclones (vm.js:16:9)

這是我對getTropicalCyclones {} (當然,我必須隱藏我的客戶ID和密碼)

const getTropicalCyclones = async () => {

    const BASE_WEATHER_API = `https://api.aerisapi.com/tropicalcyclones/`
    const CLIENT_ID_SECRET = `SECRET`
    const BASIN = `currentbasin=wp`
    const PLACE = `p=25,115,5,135` // rough coords for PH area of responsibility
    const ACTION = `within` // within, closest, search, affects or ''
    try {
        let text = ''
        let response = {}
        await axios.get(
            `${BASE_WEATHER_API}${ACTION}?${CLIENT_ID_SECRET}&${BASIN}&${PLACE}`
    )
        .then((resp) => {]
            response = resp
            text = 'Success retrieving weather!'
        })
        .catch((error) => {
            console.log('!! error', error)
        })

    const payload = await bp.cms.renderElement(
        'builtin_text',
        {
            text,
        },
        event.channel
    )
    await bp.events.replyToEvent(event, payload)
} catch (e) {
    // Failed to fetch, this is where ReferenceError: axios is not defined comes from
    console.log('!! Error while trying to fetch weather info', e)
    const payload = await bp.cms.renderElement(
        'builtin_text',
        {
            text: 'Error while trying to fetch weather info.',
        },
        event.channel
    )
    await bp.events.replyToEvent(event, payload)
  }
}

return getTropicalCyclones()

所以我的問題是, 如何導入axios? 我試過了

const axios = require('axios') 

或從“ axios”導入axios;

但這會導致另一個錯誤:

Error processing "getTropicalCyclones {}"
Err: An error occurred while executing the action "getTropicalCyclones"

查看GitHub上的package.json,看來axios已安裝https://github.com/botpress/botpress/blob/master/package.json

但是,我無法在我的機器人目錄中找到這個package.json ...

其次,基於舊版本的文檔,該示例代碼看起來像是直接使用axios的示例代碼https://botpress.io/docs/10.31/recipes/apis/

如何在Botpress上使用axios? 任何線索將不勝感激

Botpress:v11.0.0

只需使用ES6 import 在代碼頂部添加此行。

import axios from 'axios';

注意 :我期望axios已經安裝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM