[英]Is there some way to convert a json file to an object model in Deno?
問題:當我嘗試讀取文件 (student.json) 並將其存儲在 Student [] 類型的變量中時,它顯示“類型‘未知’不可分配給類型‘學生 []’。” 這是一個 typescript 文件。
import { Student } from "../Models/studentModel.ts";
import { readJson, writeJson } from "https://deno.land/std/fs/mod.ts";
const f = await readJson("../public/student.json");
const students:Student[] = f;
export const get_all_students = (ctx: Context) => {
return ctx.json(students,200);
};
期望:我正在嘗試將 json 從文件返回到服務器。 嘗試的解決方案:我嘗試過 Json.stringify()。 它仍然給我同樣的錯誤。
readJson 方法返回 promise 類型未知。 未知類型只能分配給 any 類型和未知類型本身。
如果你想強制編譯器相信你未知類型的值是給定類型的,你可以使用這樣的類型斷言:
const f = await readJson("./public/student.json");
const students:Student[] = f as Student[];
要解決上述錯誤,只需使用類型斷言:
const students = f as Student[];
我不熟悉 deno,但很確定您可以編寫文件以響應 stream 或使用適當的標頭提供它。 不確定這對您的場景是否有意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.