denodb
denodb copied to clipboard
Panic when record not found (mongo + deno 1.1.1)
I'm trying to use mongo as my database, but whenever I try to query by _id a record that doesn't exists, I get the following panic message:
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: FromHexError(OddLength)', /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/bson-0.14.1/src/bson.rs:575:39
stack backtrace:
0: 0x10ca6a30f - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hdfed66b3e2e8ce60
1: 0x10ca8c94e - core::fmt::write::h9de7b13cfaa80b03
2: 0x10ca62ff7 - std::io::Write::write_fmt::h9197012b9d65a9a3
3: 0x10ca6c1ca - std::panicking::default_hook::{{closure}}::h7b209cdc44c5777f
4: 0x10ca6bf0c - std::panicking::default_hook::hf135bc502e6f77db
5: 0x10ca6c808 - std::panicking::rust_panic_with_hook::h7967810bc33e523b
6: 0x10ca6c3d2 - rust_begin_unwind
7: 0x10caa396f - core::panicking::panic_fmt::h9eea9e4965bd189c
8: 0x10caa3875 - core::result::unwrap_failed::h21e25fa0dcab039e
9: 0x10c8d5fe0 - bson::bson::Bson::from_extended_document::h0d98e13ce03edce3
10: 0x10c8d3824 - <bson::bson::Bson as core::convert::From<serde_json::value::Value>>::from::he8abc1fed3672154
11: 0x10c8df909 - <bson::ordered::OrderedDocument as core::iter::traits::collect::FromIterator<(alloc::string::String, bson::bson::Bson)>>::from_iter::h00bbcf0b5abe5f18
12: 0x10c8d3819 - <bson::bson::Bson as core::convert::From<serde_json::value::Value>>::from::he8abc1fed3672154
13: 0x10c8df909 - <bson::ordered::OrderedDocument as core::iter::traits::collect::FromIterator<(alloc::string::String, bson::bson::Bson)>>::from_iter::h00bbcf0b5abe5f18
14: 0x10c8d3819 - <bson::bson::Bson as core::convert::From<serde_json::value::Value>>::from::he8abc1fed3672154
15: 0x10c8df909 - <bson::ordered::OrderedDocument as core::iter::traits::collect::FromIterator<(alloc::string::String, bson::bson::Bson)>>::from_iter::h00bbcf0b5abe5f18
16: 0x10c8d3819 - <bson::bson::Bson as core::convert::From<serde_json::value::Value>>::from::he8abc1fed3672154
17: 0x10c5f866a - <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter::h65b478f64390c1ee
18: 0x10c5e4475 - <std::future::GenFuture<T> as core::future::future::Future>::poll::he73d0e2568a0fe1b
19: 0x109612b59 - <futures_util::future::future::map::Map<Fut,F> as core::future::future::Future>::poll::hf01060b42de822b5
20: 0x1095f531e - <futures_util::future::future::Map<Fut,F> as core::future::future::Future>::poll::hde6d0def848c8367
21: 0x109616955 - futures_util::stream::stream::StreamExt::poll_next_unpin::h1433266551acc826
22: 0x1095f8342 - <deno_core::core_isolate::CoreIsolate as core::future::future::Future>::poll::h0913f8746ac78a46
23: 0x1095f18b1 - <deno_core::es_isolate::EsIsolate as core::future::future::Future>::poll::h09b090f5447992ff
24: 0x108c5d39f - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h362f7e9d9ab70cb6
25: 0x108b3061a - std::thread::local::LocalKey<T>::with::hb0d6015db624c190
26: 0x108c53db7 - tokio::macros::scoped_tls::ScopedKey<T>::set::h6438237492ff5021
27: 0x108d2a1a1 - tokio::runtime::basic_scheduler::BasicScheduler<P>::block_on::he1fadf525428fde9
28: 0x108cc55cc - tokio::runtime::context::enter::h3f610df64d4137a9
29: 0x108d915cc - deno::tokio_util::run_basic::hec2c91814502fdd4
30: 0x108d09961 - deno::main::h23eea4fd88e6ff95
31: 0x108b2f146 - std::rt::lang_start::{{closure}}::h8a4a860e7a4ca2d1
32: 0x10a1e4fd9 - std::rt::lang_start_internal::h1069bf3e81ece2dd
33: 0x108d0ab39 - main
This is using the where
method on the model, the find
method looks for the field "id" instead of "_id" in mongo, so if I use it, it returns no matches everytime (although no panic error message).
Im having a similar issue. Im currently trying to use "deleteById" and it doesnt seem to find the record. Also, i assumed that
id: { primaryKey: true, autoIncrement: true },
Would result in adding of custom keys in case we ever decided to change a db, so everything is consistent and there is no potential issues with handling ids, but in case of mongo that doesnt seem to be doing anything really.
EDIT: Ok, i believe i found the solution. It was a bit nasty in a way. First of all I had to add "_id" field in the model instead of the "id", as specified in the mongo documentation. That was an oversight on my part. Moreover,I have been parsing the ids to string, as the model methods only accept "FieldValue", which is
export type FieldValue = number | string | boolean | Date | null;
It turned out that this confused my poor Mongo. The solution was to overwrite the type definition so it would accept Mongos ObjectId Old code, not working code.
const user = await User.where({ name: "John" }).first(),
await Key.deleteById(JSON.stringify(user._id));
This will work
const user = await User.where({ name: "John" }).first(),
await Key.deleteById(user._id as any);
This will throw a type error indicating that we need to convert it to string
const user = await User.where({ name: "John" }).first(),
await Key.deleteById(user._id);