英文:
Error while dealing with tRPC procedures "input needs to be an object when doing a batch call" ... even it is already an object
问题
这只是一个使用FB IG Graph API来获取用户信息并将其发送到我为此目的创建的另一个网站端点的项目。我已经搜索并尝试了不同的方法,但仍然无法看出问题在哪里。
我还检查了返回的typeof(input),结果是"object",所以我无法理解为什么tRPC没有将其验证为对象。
此外,我不知道为什么,但如果我将URL更改为我构建的另一个用于接收POST请求的URL,tRPC就完全没有问题。这几天来一直在试图找出问题所在,任何帮助都非常感激!
其他信息:
- 项目部署在Vercel上。
- 我的package.json:
{
"name": "viveapp-ig-api",
"version": "0.1.0",
"private": true,
"scripts": {
"build": "next build",
"dev": "next dev",
"postinstall": "prisma generate",
"lint": "next lint",
"start": "next start"
},
"dependencies": {
"@greatsumini/react-facebook-login": "^3.3.3",
"@prisma/client": "^4.11.0",
"@t3-oss/env-nextjs": "^0.2.1",
"@tanstack/react-query": "^4.28.0",
"@trpc/client": "^10.18.0",
"@trpc/next": "^10.18.0",
"@trpc/react-query": "^10.18.0",
"@trpc/server": "^10.18.0",
"axios": "^1.4.0",
"next": "^12.3.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"recharts": "^2.7.2",
"superjson": "1.12.2",
"zod": "^3.21.4"
},
"devDependencies": {
"@types/eslint": "^8.21.3",
"@types/node": "^18.15.5",
"@types/prettier": "^2.7.2",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"autoprefixer": "^10.4.14",
"eslint": "^8.36.0",
"eslint-config-next": "^13.4.1",
"postcss": "^8.4.21",
"prettier": "^2.8.6",
"prettier-plugin-tailwindcss": "^0.2.6",
"prisma": "^4.11.0",
"tailwindcss": "^3.3.0",
"typescript": "^5.0.2"
},
"ct3aMetadata": {
"initVersion": "7.13.0"
}
}
POST请求应该完全没有问题。我还有其他使用axios执行GET请求的过程,它们没有任何问题。正如我所说,如果将URL更改为我构建的另一个用于接收POST请求的URL,tRPC根本不会抱怨。
英文:
My code, as a part of a create-t3-app
project in NextJs, tRPC and Prisma.
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { env } from "~/env.mjs";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import axios from "axios";
export const profileRouter = createTRPCRouter({
sendProfileInfo: publicProcedure
.input(
z.object({
ig_account: z.string(),
followers_count: z.number(),
audience_city: z.record(z.number()),
audience_country: z.record(z.number()),
audience_gender_age: z.record(z.number()),
audience_locale: z.record(z.number()),
})
)
.mutation(async ({ input }) => {
const axiosConfig = {
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": `${env.TOKEN}`,
},
};
try {
const response = await axios.post($env.URL, input, axiosConfig);
return {response: response.data, inputType: typeof(input)};
} catch (e) {
throw new TRPCError({
message: "Error",
code: "BAD_REQUEST",
});
}
}),
});
This is just a project that uses FB IG Graph API to retrieve information about the user and sends that to an endpoint on another website I have for that purpose. I googled and tried different approaches and I still can't see what is wrong.
I have also checked that returning typeof(input) results in "object" so I cannot understand why tRPC is not validating it as an object.
Also, I don't know why, but if I change the url to another url I build to receive POST requests I get no trouble at all. This just has been driving me crazy the past few days trying to figure it out... Any help extremely appreciated!
Other information:
- Project is deployed on Vercel.
- My package.json:
{
"name": "viveapp-ig-api",
"version": "0.1.0",
"private": true,
"scripts": {
"build": "next build",
"dev": "next dev",
"postinstall": "prisma generate",
"lint": "next lint",
"start": "next start"
},
"dependencies": {
"@greatsumini/react-facebook-login": "^3.3.3",
"@prisma/client": "^4.11.0",
"@t3-oss/env-nextjs": "^0.2.1",
"@tanstack/react-query": "^4.28.0",
"@trpc/client": "^10.18.0",
"@trpc/next": "^10.18.0",
"@trpc/react-query": "^10.18.0",
"@trpc/server": "^10.18.0",
"axios": "^1.4.0",
"next": "^12.3.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"recharts": "^2.7.2",
"superjson": "1.12.2",
"zod": "^3.21.4"
},
"devDependencies": {
"@types/eslint": "^8.21.3",
"@types/node": "^18.15.5",
"@types/prettier": "^2.7.2",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"autoprefixer": "^10.4.14",
"eslint": "^8.36.0",
"eslint-config-next": "^13.4.1",
"postcss": "^8.4.21",
"prettier": "^2.8.6",
"prettier-plugin-tailwindcss": "^0.2.6",
"prisma": "^4.11.0",
"tailwindcss": "^3.3.0",
"typescript": "^5.0.2"
},
"ct3aMetadata": {
"initVersion": "7.13.0"
}
}
Post request should be done without any issues at all. I have other procedures that use axios too to do GET requests and they have no problem at all. Even as said, if I change the URL to another I built to receive POST requests tRPC does not complain at all.
答案1
得分: 0
如果将代码行:
const response = await axios.post($env.URL, input, axiosConfig)
改成
await axios.post($env.URL, input, axiosConfig)
而不等待响应并将其分配给变量,那么它就可以正常工作(我不知道为什么,但这样修复了bug)。
英文:
In case anyone struggles with this in the future... I found the bug.
It all lies on this line of code:
const response = await axios.post($env.URL, input, axiosConfig)
that needs to become await axios.post($env.URL, input, axiosConfig)
without awaiting the response to assign it to a variable. If it is approached that way then it works just fine (idk why, just fixed the bug).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论