-1

can anyone tell me why type of dbJson returning string it should return object

export async function POST(req: Request) {
  const data = (await req.json()) as { type: "print" | "view" | "save" };
  let db = await ReadDb();
  let dbJson = JSON.parse(db) as dbSchema;
  console.log(typeof dbJson);
  if (data.type === "print") {
    dbJson.printResume = dbJson.printResume + 1;
  } else if (data.type === "save") {
    dbJson.savePdf = dbJson.savePdf + 1;
  } else if (data.type === "view") {
    dbJson.pageView = dbJson.pageView + 1;
  }
  SaveDb(dbJson);
  return NextResponse.json({ status: "Ok" });
}
async function ReadDb() {
  const jsonDirectory = path.join(process.cwd(), "json");
  const db = await fs.readFile(process.cwd() + "/public/db.json", "utf-8");

  return db;
}
async function SaveDb(newDb: dbSchema) {
  const db = await fs.writeFile(
    process.cwd() + "/public/db.json",
    JSON.stringify(newDb)
  );
}

console

string
- error TypeError: Cannot create property 'printResume' on string '{"pageView":0,"printResume":0,"savePdf":0}'
    at POST (webpack-internal:///(sc_server)/./app/api/views/route.ts:26:28)
    at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:242:37)

can anyone tell why this is happening how can I resolve this issue

1 Answers1

0

At all every thing is on printed log bro

'{"pageView":0,"printResume":0,"savePdf":0}'

It is a string :)

All is related to ReadDb() function

I think you use sqlite, and sqlite dont support json and object

Radmehr
  • 299
  • 2
  • 12
  • ```js let dbJson = JSON.parse(db) as dbSchema; ``` i am not using sqlite i am using json file for db and I don't know how but it start working – Pradeep Bisht Jun 02 '23 at 05:23