lvin
lvin13mo ago

not sure why this code is erroring

for (const number of genNumbers()) {
const result = await fetchPages(number);
if (result instanceof Error) break;
const urls = parse(result);
await Promise.all(
urls.map((url) =>
fetchPage(`https://test.nl${url}`).then(({ name, videos }) =>
Deno.writeTextFile(name, JSON.stringify(videos))
)
)
);
}
for (const number of genNumbers()) {
const result = await fetchPages(number);
if (result instanceof Error) break;
const urls = parse(result);
await Promise.all(
urls.map((url) =>
fetchPage(`https://test.nl${url}`).then(({ name, videos }) =>
Deno.writeTextFile(name, JSON.stringify(videos))
)
)
);
}
Task generate deno run -A ./scripts/generate.ts
error: Uncaught TypeError: Expected string at position 1
Deno.writeTextFile(name, JSON.stringify(videos))
^
at writeFile (ext:deno_fs/30_fs.js:807:18)
at Object.writeTextFile (ext:deno_fs/30_fs.js:850:12)
at file:///home/al/Code/crookedy/scripts/generate.ts:76:14
at eventLoopTick (ext:core/01_core.js:183:11)
at async Promise.all (index 10)
at async file:///home/al/Code/crookedy/scripts/generate.ts:73:3
Task generate deno run -A ./scripts/generate.ts
error: Uncaught TypeError: Expected string at position 1
Deno.writeTextFile(name, JSON.stringify(videos))
^
at writeFile (ext:deno_fs/30_fs.js:807:18)
at Object.writeTextFile (ext:deno_fs/30_fs.js:850:12)
at file:///home/al/Code/crookedy/scripts/generate.ts:76:14
at eventLoopTick (ext:core/01_core.js:183:11)
at async Promise.all (index 10)
at async file:///home/al/Code/crookedy/scripts/generate.ts:73:3
9 Replies
NDH
NDH13mo ago
may have to await the parse(result)?
lvin
lvin13mo ago
no that function is synchrous, and using writeTextFileSync doesn't help either
Esente
Esente13mo ago
Can you check if you actually get a string for name? The error says that the first argument for writeTextFile is not a string.
lvin
lvin13mo ago
horrible error message
Mr.Possumz
Mr.Possumz13mo ago
If Promise.all() is expecting an iterable of promises, technically what you're returning is a composition of a promise and a chained statement. I don't often use Promise.all but I'm betting that fetchPage is generating the promise and passing the promise to the chained .then() statement, which fails to destructure the name and videos variables. You probably just need to declare the map callback as async to generate a promise.
lvin
lvin13mo ago
nah, functions only need to be tagged w/ async if im using await inside them. the map converts all the urls Array<string> to Array<Promise<{name:string,videos:Array<Video>}>> hence why i can use promise.all with it the name parameter for Deno.writefile was undefined instead of a string so thats why shit borked
Kyiro
Kyiro13mo ago
console log videos
NeTT
NeTT13mo ago
That was a poor error message indeed. Might need to open an issue for enhancement.
ybabts
ybabts13mo ago
It would be helpful to share the functions you're using inside your snippet. Whatever object you're destructuring in the then statement, the name property isn't always a string, hence the error. You should log the output of the object you're destructuring to confirm this. The solution would be to type check name and videos to ensure that they are the proper types before attempting to write them to a file. Also I am interested in what this is here
const result = await fetchPages(number);
if (result instanceof Error) break;
const result = await fetchPages(number);
if (result instanceof Error) break;
More Posts
Why doesn't POST response from serveTls support streaming chunks from a ReadableStream?We can POST a ReadableStream to a serveTls server, however when we respond with a ReadableStream theWhen I use npm: node: specifiers how can I specify the version?We know in Deno we can do `import { Client, Pool } from "https://deno.land/x/pg@v0.6.1/mod.ts";` I wFakeTime seems to break async testsI am attempting to use FakeTime similarly to in the Faking time example of https://deno.land/manual@How to serve HTTPS with the Deno.serve() APII used to start a dev server with TLS like so ```const server = Deno.listenTls({ port: 7000, trCustom console.log formatting for a class/objectIs there a way to change the way an object/class is printed in the console? For example, instead ofIs there a way to write to a file by replacing bytes?Like "I am a looong file" > write('Test') > 'Test a looong file'std/http/server: URI too long —how to avoid loading those?Using https://deno.land/std@0.194.0/http/server.ts, is there a way to reject long URIs before they aWorker: TS2304 [ERROR]: Cannot find name 'postMessage'When type-checking a worker script that uses the global `postMessage()` method, `deno check` gives aHow to recover from worker death? It terminates my main program…In the error message handler, I replace the dead worker with a new one, but it kills my program eithIs there anyone using kv with pentagon ORM and Zod ?I've tried using pentagon for Deno kv in a Fresh project, and it seems like it's not working.