【发布时间】:2022-01-07 03:33:40
【问题描述】:
我知道在 SO 上有一个同样错误的问题,但那里的答案对我没有帮助:deno bundle failed. Property 'getIterator' does not exist on type 'ReadableStream<R>'
这是完整的错误:
❯ deno run --allow-all server.ts 检查文件:///Users/hagenek/repos/mock-backend/server.ts 错误:TS2339 [错误]:“ReadableStream”类型上不存在属性“getIterator”。 返回 res.readable.getIterator(); ~~~~~~~~~~~ 在https://deno.land/std@0.83.0/async/pool.ts:45:23
这是我的 server.ts 代码:
import { Application } from "./deps.ts";
import router from "./routes.ts"
const PORT = 4000;
const app = new Application();
app.use(router.routes()); // Pass our router as a middleware
app.use(router.allowedMethods()); // Allow HTTP methods on router
await app.listen({ port: PORT });
console.log(`Server running on PORT: ${PORT}`)
Routes.ts:
import { Router } from "https://deno.land/x/oak/mod.ts";
import {
addQuote,
getQuotes,
getQuote,
updateQuote,
deleteQuote,
} from "./controllers/controller.ts";
interface ReadableStream<R> {
getIterator(): any
}
const router = new Router(); // Create Router
router
.get("/api/quote", getQuotes) // Get all quotes
.get("/api/quote/:id", getQuote) // Get one quote of quoteID: id
.post("/api/quote", addQuote) // Add a quote
.put("/api/quote/:id", updateQuote) // Update a quote
.delete("/api/quote/:id", deleteQuote); // Delete a quote
export default router;
Deps.ts
export {
Application
} from "https://deno.land/x/oak/mod.ts"
Controller.ts
interface Quote {
_id: { $oid: string };
quote: string;
quoteID: string;
author: string;
}
import {MongoClient} from "https://deno.land/x/mongo@v0.22.0/mod.ts";
const URI = "mongodb://127.0.0.1:27017";
// Mongo Connection Init
const client = new MongoClient();
try {
await client.connect(URI);
console.log("Database successfully connected");
} catch (err) {
console.log(err);
}
const db = client.database("quotesApp");
const quotes = db.collection<Quote>("quotes");
// DESC: ADD single quote
// METHOD: POST /api/quote
export const addQuote = async ({request, response,}: {
request: any;
response: any;
}) => {
try {
// If the request has no Body, it will return a 404
if (!request.hasBody) {
response.status = 400;
response.body = {
success: false,
msg: "No Data",
};
} else {
// Otherwise, it will try to insert
// a quote in the DB and respond with 201
const body = await request.body();
const quote = await body.value;
await quotes.insertOne(quote);
response.status = 201;
response.body = {
success: true,
data: quote,
};
}
} catch (err) {
response.body = {
success: false,
msg: err.toString(),
};
}
};
// DESC: GET single quote
// METHOD: GET /api/quote/:id
export const getQuote = async ({
params,
response,
}: {
params: { id: string };
response: any;
}) => {
// Searches for a particular quote in the DB
const quote = await quotes.findOne({quoteID: params.id});
// If found, respond with the quote. If not, respond with a 404
if (quote) {
response.status = 200;
response.body = {
success: true,
data: quote,
};
} else {
response.status = 404;
response.body = {
success: false,
msg: "No quote found",
};
}
};
// DESC: GET all Quotes
// METHOD GET /api/quote
export const getQuotes = async ({response}: { response: any }) => {
try {
// Find all quotes and convert them into an Array
const allQuotes = await quotes.find({}).toArray();
console.log(allQuotes);
if (allQuotes) {
response.status = 200;
response.body = {
success: true,
data: allQuotes,
};
} else {
response.status = 500;
response.body = {
success: false,
msg: "Internal Server Error",
};
}
} catch (err) {
response.body = {
success: false,
msg: err.toString(),
};
}
};
// DESC: UPDATE single quote
// METHOD: PUT /api/quote/:id
export const updateQuote = async ({
params,
request,
response,
}: {
params: { id: string };
request: any;
response: any;
}) => {
try {
// Search a quote in the DB and update with given values if found
const body = await request.body();
const inputQuote = await body.value;
await quotes.updateOne(
{ quoteID: params.id },
{ $set: { quote: inputQuote.quote, author: inputQuote.author } }
);
// Respond with the Updated Quote
const updatedQuote = await quotes.findOne({ quoteID: params.id });
response.status = 200;
response.body = {
success: true,
data: updatedQuote,
};
} catch (err) {
response.body = {
success: false,
msg: err.toString(),
};
}
};
// DESC: DELETE single quote
// METHOD: DELETE /api/quote/:id
export const deleteQuote = async ({
params,
response,
}: {
params: { id: string };
request: any;
response: any;
}) => {
try {
// Search for the given quote and drop it from the DB
await quotes.deleteOne({quoteID: params.id});
response.status = 201;
response.body = {
success: true,
msg: "Product deleted",
};
} catch (err) {
response.body = {
success: false,
msg: err.toString(),
};
}
};
【问题讨论】:
-
我看不到您尝试在哪里使用
res.readable.getIterator()(来自您问题标题中的错误消息)。您是否还可以在./controllers/controller.ts中包含模块的代码(以及任何相关代码,如果相关)?能够重现您的问题将有助于确定解决方案。 -
补充了更多信息,希望对您调试有帮助。
标签: dependencies deno