I'm trying to update a User's discord ID based on their key to link the two. Here is my code:
console.log(user.discordID);
User.updateOne({key: user.key},{discordID: user.discordID}, (err, result) => {
if(err){
console.log(err);
return err;
}
console.log("Updated missing User discord ID");
console.log(result);
console.log(user.discordID);
});
I pass in the user object to the function and have verified the values are correct. The console.logs show the correct value, 660955020694650891, both before and after the update. But in the DB itself after the update the value is 660955020694650900. For the life of me I can't figure out why this is happening and it is derailing other functionality. Any help would be greatly appreciated.
Its probably because the number of the discordID is bigger then the 'Number.MAX_SAFE_INTEGER', which means that javascript can not represent the exact number.
See more information here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
You can solve the problem by using BigInt.
See details here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
Related
I'm working on a Node.js app that uses statuses/show/:id to fetch a Tweet object via its ID. However, I keep getting the following error.
"Sorry, that page does not exist', code: 34"
I've been looking for solutions, but they all usually lead back to the GET method being written incorrectly. I've double checked, though, and am pretty sure I'm writing it correctly.
T.get('statuses/show/:id', { id: '759043035355312128' }, function(err, data, response) {
console.log(err);
console.log(data);
});
I've also tried inputting the ID as an int, to no avail.
Did you check if the reference to the object is correct/ try absolute path instead of relative and see if it fixes it.
Newbie in nodejs/mongoDB here. I've tried, with no success,
to find the answer to my problem before posting here.
I'm creating a simple node RESTAPI get services with Mongoose. I'm trying to pass the field value of the collection to retrieve a specific document.
Like this http://localhost:3000/infrakpi/fieldvalue in the browser
I've written this following piece of code.
app.get('/infrakpi/:system',(req, res) => {
Infrakpi.getInfrakpiBySystem(req.params.system, function(err, infrakpibysystem){
if (err) {
throw err;
}
res.json(infrakpibysystem);
});
});
I have defined the get method in my mongoose model like below.
//Get Infrakpi by System
module.exports.getInfrakpiBySystem = function (system, callback) {
Infrakpi.find({system: 'system'}, callback)
}
when system is passed as fieldvalue in the restURL, I want to retrieve the specific document in the collection.
I understand that this may be very basic question but I get result when I use findById for _id field. But client will call only with the specific field.
Appreciate your help.
Not Sure if i can call it stackoverflow luck. I overlooked the quotes in get method in the model. Once I removed them, it worked.
//Get Infrakpi by System
module.exports.getInfrakpiBySystem = function (system, callback) {
Infrakpi.find({system: system}, callback)
}
I'm leaving the question here without deleting, if it can help someone else in the future.
I was looking at some tutorial, there I found a piece of code where I stuck.Please help me to understand this code.I have marked the questions in my comments.
Code
UserSchema.pre('save', function(next){ //this is a pre hook which is used.understood.
var user = this; // what is the function of this?
var SALT_FACTOR = 5;
if(!user.isModified('password')){ //not understood.From where this function arises?I did not found this anywhere in mongoose tutorial/api.
return next();
}
bcrypt.genSalt(SALT_FACTOR, function(err, salt){
if(err){
return next(err);
}
A "pre-save" middleware in Mongoose is "document middleware".
The documentation states:
...in document middleware, this refers to the document being updated.
So this refers to the document to be saved.
This also provides a clue as to what isModified is: it's a document method that can be used to check if a particular field, password in this case, has been modified since the document was retrieved from the database earlier.
In the code you're posting, if the password hasn't been changed, there's not need to hash it again (using bcrypt), so that step is skipped by calling next and returning from the middleware.
isModified is documented here: http://mongoosejs.com/docs/api.html#document_Document-isModified
Feel free to let me know if this isn't a common practice - I'm a fairly new programmer - but I thought I've seen APIs in the past that, when you submit a DELETE request to a resource (/todo/1234), some servers will return the object you just deleted in the response. Is that a thing? If so, I'd be interested in learning how to do it. Here's what I have:
.delete(function (req, res) {
Todo.findById(req.params.todoId).remove(function (err) {
if (err) res.status(500).send(err);
res.send("Todo item successfully deleted");
});
});
This code does delete the item, but I would like to return the item that got deleted in the response instead of a string message. If that's a normal/okay thing to do. If it isn't normal or okay for some reason, please let me know why and I'll just move on. Or perhaps there's a more common way.
This is what I found in the [RFC 7231 docs][1]:
If a DELETE method is successfully applied, the origin server SHOULD
send a 202 (Accepted) status code if the action will likely succeed
but has not yet been enacted, a 204 (No Content) status code if the
action has been enacted and no further information is to be supplied,
or a 200 (OK) status code if the action has been enacted and the
response message includes a representation describing the status.
I'm having a hard time interpreting what the 200 response means - is it only kosher to send a string message (Success!) or an object containing a message attribute ({message: "Success!"})? Or can you do whatever you want there? What's the best practice in Express using Mongoose?
Thanks in advance for the help, and sorry for my noobness with HTTP stuff.
[1]: https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5
You should use findOneAndRemove! Something like:
Todo.findOneAndremove({ id: req.params.todoId }, function( error, doc, result) {
// it will be already removed, but doc is what you need:
if (err) res.status(500).send(err);
res.send(doc.id);
});
I'm creating a little REST API with express and mongoose, and my problem is relatively simple:
I need to know the typeError to send an statusCode and message to the client. Example, well, if a query for username: John Doe is false, how i know it? Exist a typeError for not matching querys? Or it returns me an empty object instead?
Thanks, and sorry about my bad english.
The following code explains how to handle finding duplicates(or at least how I've done it). It's up to you to determine the functions you'll implement for each response and really which query you use. To answer your question though, mongo returns an empty object if there is no duplicate.
Model.findOne({ user_id: 'John Doe' }, function(err, user) {
if (err) {conole.log(err) // do something if there is an error}
if (user) {
// Do something here if a match is found
} else {
// Do something here if nothing is found.
});