Monogdb update function works on terminal but not on metoer server - mongodb

I'm trying to figure out what I'm doing wrong here with absolutely no luck.
db.attendances.update({ _id: 'hRs6LfAqPBmy4ZNuH' }, { $set: { absentParentOrGuardianDate: undefined } })
If I run this update command in the terminal shell using 'meteor mongo' absentParentOrGuardianDate is removed from the document, however, if I run the same code, slightly changed for meteor, on the meteor server I get an error.
Attendances.update({ _id: 'hRs6LfAqPBmy4ZNuH' }, { $set: { absentParentOrGuardianDate: undefined } });
The error is:
{isClientSafe: true, error: 500, reason: "Internal server error", details: undefined, message: "Internal server error [500]", …}
Can someone please tell me what I'm missing here.

import { _ } from 'meteor/underscore';
const updateDoc = {};
_.each(yourObj, (value, key) => { // I'm using underscore to help loop through the object
if (value) {
updateDoc.$set[key] = value;
} else {
updateDoc.$unset[key] = value;
}
});
yourCollection.update(yourSelector, updateDoc);

Related

MongoError, ReferenceError: db is not defined

This is an intentionally vulnerable lab to study nosql injection, the specific case of $where
Code:
let username = req.query.username;
query = { $where: `this.username == '${username}'` }
User.find(query, function (err, users) {
if (err) {
// Handle errors
} else {
res.render('userlookup', { title: 'User Lookup', users: users });
}
});
So far I managed to execute javascript code, example:
user/lookup?username=';sleep(5000);'
My next intentions are to try to collect the data inside the dbs, since $where let me execute stuff.
So, If I do for example:
user/lookup?username=';db.getMongo().getDBNames();'
I get this error:
{"ok":0,"code":139,"codeName":"JSInterpreterFailure","name":"MongoError"}
Reading the server logs:
...
cmd":{"find":"user","filter":{"$where":"this.username ==
'';db.getMongo().getDBNames();''"},"projection":{},"lsid":{"id":{"$uuid":"d910e28c-
7303-45b2-8888-2defbb220e0a"}},"$db":"vulnerablemean"}}}
node_1 | { MongoError: Executor error during find command :: caused by ::
ReferenceError: db is not defined :
...
But if in the mongo shell I run the same string, I get a result:
> db.getMongo().getDBNames()
[ "admin", "config", "local", "vulnerablemean" ]
I would like to undestand why it happens. I mean, is it not actually referenced?
How can I get the same result via injection?

Sequelize (Postgres) always 'returning' null for second index (whether creating or updating)

Trying to correctly troubleshoot HTTP status codes and responses on upsert. I've scoured SO and google to find examples of this behavior, but the answers are always really old (pre Sequelize 6 era).
From what I gather over at Sequelize docs, UPSERT returns Promise<Model,boolean | null> and additionally For SQLite/Postgres, created value will always be 'null'. But apparently for me, whether it created/didn't update/updated, it always returns null
Am I correctly understanding that boolean return will return for update(T)/no update(F) and null for creation? Or will it only return null whether create|update|no update for postgres users?
This is MY expected return behavior:
data: [
{
id: 64920,
...etc...
},
null // null = created
],
data: [
{
id: 64921,
...etc...
},
false // false = no update (data is the same)
],
data: [
{
id: 64922,
...etc...
},
true // true = update complete (some data was new)
]
Sample controller upsert command:
exports.createOne = async (req, res, next) => {
Trip.upsert({
id: req.body.id,
...etc...
},{returning: true } // not needed, default in Sequelize =< 6.0
)
.then(function (test) {
if (test) {
res.status(200);
res.send("Successfully stored");
} else {
res.status(200);
res.send("Successfully inserted");
}
})
.catch((err) => {
res.status(500).json({ error: err.message });
});
};
Anyone have examples or suggestions to properly implement this?
Thank you!

why am i getting TypeError: Cannot read property 'getText' of undefined error even though my web element reference is correct?

I'm getting an error returned when i run a protractor test.
Here is the reference to the webelement in the directories.page.js file:
get importErrorsList(){
return element.all(by.css('[ng-if="error.error.detailMessage"]'));
}
Here is a screenshot of the webelement and it's source info:
And finally here is the step that i am using to reference the webelement, which is returning the error:
Then(/^The list of import errors contains this error message: "([^"]*)"$/, function (errorText, callback) {
browser.wait(EC.visibilityOf(importPageObj.alertMsg), timeouts.EC_TIMEOUT).then(function(){
browser.wait(() => {
//the next line causes the error
expect(directoriesPageObj.importErrorsList.getText()).to.eventually.contain(errorText).and.notify(callback);
}, timeouts.EC_TIMEOUT).then(() => {
browser.wait(EC.and(EC.visibilityOf(importPageObj.headerDropDownInWebView), EC.elementToBeClickable(importPageObj.headerDropDownInWebView)), timeouts.EC_TIMEOUT).then(() => {
callback();
});
});
});
});
could anyone help me figure out why this isn't working?
you did not initialise directoriesPageObj. So, before using it you should make:
const directoriesPageObj = new DirectoriesPageObj()
and after it you can use directoriesPageObj variable. For example:
Then(/^The list of import errors contains this error message: "([^"]*)"$/, function (errorText, callback) {
browser.wait(EC.visibilityOf(importPageObj.alertMsg), timeouts.EC_TIMEOUT).then(function(){
browser.wait(() => {
const directoriesPageObj = new DirectoriesPageObj();
expect(directoriesPageObj.importErrorsList.getText()).to.eventually.contain(errorText).and.notify(callback);
}, timeouts.EC_TIMEOUT).then(() => {
browser.wait(EC.and(EC.visibilityOf(importPageObj.headerDropDownInWebView), EC.elementToBeClickable(importPageObj.headerDropDownInWebView)), timeouts.EC_TIMEOUT).then(() => {
callback();
});
});
});
});

Getting "Invalid exit definition" on Compilation of Sails Helper (Sails v1.0)

I'm getting the error
Invalid exit definition ("success"). Must be a dictionary-- i.e. plain JavaScript object like `{}`.
Invalid exit definition ("error"). Must be a dictionary-- i.e. plain JavaScript object like `{}`.
when doing sails lift. The error is on getRole.js
module.exports = {
friendlyName: 'Get Role',
description: '',
inputs: {
user_id: {
friendlyName: 'User Id',
description: 'The ID of the user to check role',
type: 'string',
required: true
}
},
exits: {
success: function (role){
return role;
},
error: function (message) {
return message;
}
},
fn: function (inputs, exits) {
User.findOne({ id: inputs.user_id } , function (err, user) {
if (err) return exits.err(err);
return exits.success(user.role);
});
}
};
This is a new error, and looking at my git, nothing has changed in my code since it successfully compiled. I understand the Sails version (v1.0) I'm using in beta, so I'm taking that into account.
Exits cannot be defined as functions. There is a special syntax (Machine Spec) to define exits. In your example this should work:
exits: {
error: {
description: 'Unexpected error occurred.',
},
success: {
description: 'Role was succesffuly fetched'
}
},
You can read more info about helper exits here: https://next.sailsjs.com/documentation/concepts/helpers
May changes occur on the last release 1.0.0-38. I've not checked underneath yet, but the way to execute helpers changed: on .exec() I get errors. Now, use .switch();

Mongoose select,populate and save behaving differently on Mac and Windows

Here's what i did
static populateReferralLinks(){
return Promise.coroutine(function*(){
let companies = yield Company.find({},'billing referral current_referral_program')
.populate('billing.user','emails name');
for(let i = 0 ; i < length ; i++){
companies[i].referral.is_created = true;
companies[i].referral.referral_email = companies[i].billing.user.emails[0].email;
companies[i] = yield companies[i].save();
}
return companies;
}).apply(this)
.catch((err) => {
throw err;
});
}
I have a funciton in which i am selecting only 3 fields to go ahead with i.e billing,current_referral_program and referral.
And populating user using the reference stored in billing.user.
Now when i call this function then on line
companies[i].save();
The following command is shown in the terminal in windows
Mongoose: companies.update(
{ _id: ObjectId("58d12e1a588a96311075c45c") },
{ '$set':
{ billing:
{ configured: false,
user: ObjectId("58d12e16588a96311075c45a") },
referral:
{ is_created: true,
referral_email: 'jadon.devesh98#gmail.com',
},
updatedAt: new Date("Wed, 22 Mar 2017 12:02:55 GMT")
}
}
)
But in Mac's terminal it shows this command
Mongoose: companies.update({ _id: ObjectId("58d12e1a588a96311075c45c") }) { '$set': { billing: { configured: false, user: ObjectId("58d12e16588a96311075c45a") }, current_limit: {}, current_usage: {},referral: { is_created: true, referral_email: 'jadon.devesh98#gmail.com'}}, '$unset': { updatedAt: 1 } }
Now, I haven't mentioned current_limit and current_usage to be empty. it's executing fine on windows but on Mac it's setting current_limit and current_usage empty thus updating my document with empty objects on Mac but not on windows.
It should behave same way on both OS but it is not.
Apparently this problem was there in Mongoose 4.5.8 and is resolved in the latest version i.e 4.9.1
Check it here