类型错误:函数缺少 1 个必需的位置参数:'self'



我在GCP中有一个函数,我在本地机器上使用它,但当我将它推到GCP云函数上时,我不断收到错误。

该函数从PubSub收集消息,然后使用为API调用收集的ID。

该函数的代码为:

from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
import requests
import json
from firebase_admin import firestore
db = firestore.Client(project='xxxx')
# API INFO
Base_url = 'https://xxxx/v1/feeds/sportsbookv2'
Sport_id = '1000093204'
AppID = 'xxxx'
AppKey = 'xxxx'
Country = 'en_AU'
Site = 'www.123.com.au'
project_id = "xxxx"
subscription_id = "xxxx_au_basketball_nba_events"
timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message):
print(message.data)
event_ids = json.loads(message.data).strip('][').split(', ')
url = f"{Base_url}/betoffer/event/{','.join(map(str, event_ids))}.json?app_id={AppID}&app_key={AppKey}&local={Country}&site={Site}"
print(url)
windata = requests.get(url).text
windata = json.loads(windata)
for odds_data in windata['betOffers']:
if odds_data['betOfferType']['name'] == 'Head to Head' and 'MAIN' in odds_data['tags']:
event_id = odds_data['eventId']
home_team = odds_data['outcomes'][0]['participant']
home_team_win_odds = odds_data['outcomes'][0]['odds']
away_team = odds_data['outcomes'][1]['participant']
away_team_win_odds = odds_data['outcomes'][1]['odds']
print(f'{event_id} {home_team} {home_team_win_odds} {away_team} {away_team_win_odds}')
# WRITE TO FIRESTORE
doc_ref = db.collection(u'xxxx_au').document(u'basketball_nba').collection(u'win_odds').document(
f'{event_id}')
doc_ref.set({
u'event_id': event_id,
u'home_team': home_team,
u'home_team_win_odds': home_team_win_odds,
u'away_team': away_team,
u'away_team_win_odds': away_team_win_odds,
u'timestamp': firestore.SERVER_TIMESTAMP,
})

subscriber.subscribe(subscription_path, callback=callback)
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}..n")
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel()  # Trigger the shutdown.
streaming_pull_future.result()  # Block until the shutdown is complete.

在我本地机器的终端中,它似乎可以工作。输出低于

b'"[1018936430, 1018936341, 1018936411, 1018936397, 1018936472, 1018936410, 1018936333, 1018936326, 1018936349, 1018936381, 1018936393]"'
['1018936430', '1018936341', '1018936411', '1018936397', '1018936472', '1018936410', '1018936333', '1018936326', '1018936349', '1018936381', '1018936393']
https://xxx/v1/feeds/sportsbookv2/betoffer/event/1018936430,1018936341,1018936411,1018936397,1018936472,1018936410,1018936333,1018936326,1018936349,1018936381,1018936393.json?app_id=21e03bb8&app_key=c0191eaf354cc07bc3769817b8c1acd0&local=en_AU&site=www.xxx.com.au
1018936397 New York Knicks 1830 Atlanta Hawks 2020
1018936472 Chicago Bulls 1450 Charlotte Hornets 2800
1018936349 Dallas Mavericks 1450 Utah Jazz 2800
1018936411 Miami Heat 1380 Sacramento Kings 3100
1018936430 Philadelphia 76ers 1300 Washington Wizards 3550
1018936410 Houston Rockets 2500 Los Angeles Clippers 1540
1018936333 Milwaukee Bucks 1170 Detroit Pistons 5400
1018936381 Portland Trail Blazers 2330 Memphis Grizzlies 1610
1018936393 Los Angeles Lakers 2250 New Orleans Pelicans 1650
1018936341 Cleveland Cavaliers 2040 Boston Celtics 1790
1018936326 San Antonio Spurs 3550 Toronto Raptors 1300

当我在GCP云函数中设置此函数时,我在日志中不断收到以下错误,它崩溃了。

TypeError: callback() missing 1 required positional argument: 'self'

当我将self放入回调def的((中时,我再次出现错误。

我在这里错过了什么?

如果在调用类方法之前没有实例化类的对象,则通常会引发missing 1 required positional argument:self错误。当您错误地实例化类时,也会引发此错误
若要解决此错误,请确保在尝试访问某个类的任何方法之前,先实例化该类的对象。然后,检查以确保使用正确的语法实例化对象。正如John Hanley在上面的评论中所提到的,建议您通过函数代码之外的触发器订阅Pub/Sub。

在这里查看类似的示例:

  1. 缺少一个必需的位置参数self
  2. 调用和使用类方法的正确方法
  3. 缺少类型错误