If you aren't phone anxious, I would recommend calling Nintendo support. They'll ask you for the error code. A similar issue happened to me months back while I was trying to buy/ trade something right here on the forums. I could go to my boyfriends island on his switch via online and local; but whenever I tried to invite anyone else in or go to the trader's town it gave me an error message. My BF had recently switched routers (I think it was that, I am not technologically literate) and it was a NAT type issue.
It was all pretty confusing, but a super patient and kind rep on the phone walked me through it; I had to call back a time or two because sometimes the solution I thought worked didn't. We did eventually fix it by going into the network settings of our router (correct me if it's the modem, tech people) and allowing our switches to access it or something like that.
I really hope I was able to give some insight? If not I am so sorry.