node.js


Express with mongodb find one


Im new to nodejs , express and mongodb.
I got stuck at the findOne function using ObjectId of mongodb
With the code below, I got the error : "Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"
Im using lastest version of everything (because Im new to them)
My code in view:
<% for(var i = 0 ; i < posts.length; i++ ) { %>
<% post = posts[i] %>
<article class="post">
<div class="post-preview col-xs-10 no-gutter">
<h2>
<a href="/posts/<%=i%>">
<%= post.title %>
</a>
</h2>
<p><%= post.description %></p>
<p class="meta">
<%= post.author.name %> in
<%= post.category.name %> <i class="link-spacer"></i> <i class="fa fa-bookmark"></i> <%= post.created_at %>
</p>
</div>
<div class=" col-xs-2 no-gutter">
<img src="<%= post.author.image %>" class="user-icon" alt="user-image">
</div>
</article>
<% } %>
Please tell me what's wrong with my code .
p/s : the req.params.id is valid and logable.
Default mongo IDs don't increment from 1. They will look like "_id" : ObjectId("5908f94c06515dfa8522459c") in the database. Your problem is your href is navigating by index, not the id itself. You need to change:
<a href="/posts/<%=i%>">
<%= post.title %>
</a>
to:
<a href="/posts/<%=post._id%>">
<%= post.title %>
</a>
this will make your link /posts/5908f94c06515dfa8522459c instead of /posts/1
I'm guessing it's because the id is a string of not 12 bytes (nor 24 hex characters). When the URL comes in to your express web server, it's a string. When express matches a part of it and stores it, there's no rule that :id couldn't be myCoolId, which isn't a number.
So everything in req.params is a string. I would add some checking to make sure it's a number, cast it, and then give it to ObjectId.
EDIT: If you want to work with it as strings, then try some of the following:
const ObjectID = require('mongodb').ObjectID;
// When making a brand new record, just use a new object id.
let record = new ObjectID();
console.log(record);
// When giving the ID to the client as a string, make it a hex string.
let hexString = record.toHexString();
console.log(hexString);
// You can validate that it's a valid object id in your route when they post it back.
let valid = ObjectID.isValid(hexString);
console.log(valid);
// Then you can turn it back into an object id.
let fromHex = ObjectID.createFromHexString(hexString);
console.log(fromHex);

Related Links

NodeJS - Why is node reporting wrong version..?
Chrome Devtools with no internet connection
electron - process.env.npm_package* variables missing
Mongoose: only show populated results
Woocommerce API Variable Subscription Product update
Alexa Skill - Keep Intent alive for dialogue
Post to google+ wall from plusDomains api
Nodemailer error connect ECONNREFUSED
Node.js not serving files other than index.html
do not run loop queue when specify argument in yeoman generator
cannot pass variables from node js to ejs page
Node.js: how to save uploaded file from browser form?
How to put flash message into my code?
Aggregate method not working as expected
Docker compose version 3 doesn't work as I expected
Ajax call in react js

Categories

HOME
solr
okta
masonry
properties
refactoring
environment-variables
asp-classic
g2o
freeswitch
uwp-xaml
caffe
keyboard-shortcuts
login
ais
asp.net-web-api2
geoserver
netfilter
playframework-2.5
sorm
rhel
apigee
treeset
mit-scratch
corba
m3u8
fix
watchkit
border
intercept
packet-capture
workspace
ext4
footable
quickbooks-online
gunicorn
basic4android
leanft
filestream
actor
xilinx-edk
scalacheck
google-truth
carplay
javac
gear-vr
oltp
orders
rtai
spring-social-facebook
google-chartwrapper
stdio
vibration
countdowntimer
dotpeek
iis-8.5
rc4-cipher
ibm-jazz
rgraph
ng-template
rebase
episerver-6-r2
johnny-five
traffic-simulation
max-heap
du
beta-testing
merit-gem
openembedded
sql-agent-job
mpmediapickercontroller
joptimizer
qmediaplayer
gamma-function
jquery-dropkick
sequel
curator
virtual-address-space
spool
function-prototypes
nxt-python
textile
openacs
pixate
contactscontract
where-in
uipicker
jquery-ui-selectable
django-staticfiles
array-formulas
circusd
strtotime
nservicebus3
visual-studio-test-runner
launching-application
umbraco5
web-setup-project
cloning
eda
xcode4.1
validationsummary
clean-language

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile