I want "value" from the following query string.
by doing request.query.filter i get the following
"[{\"property\":\"customerId\",\"value\":2,\"exactMatch\":true}]"
tried request.query.filter.value and request.query.filter["value"] but didn't work.
Request URL :
admin/api/login?action=get&_dc=1547652537836&filter=%5B%7B%22property%22%3A%22customerId%22%2C%22value%22%3A2%2C%22exactMatch%22%3Atrue%7D%5D
the query string seems to be a JSON string. so the first thing you have to do is convert it to json object to access it.
const query = "[{\"property\":\"customerId\",\"value\":2,\"exactMatch\":true}]";
const json = JSON.parse(query);
now you can access it with
json[0].value
Related
{
acknowledged: true,
insertedId: new ObjectId("612d9d08db9c20f031033478")
}
This was the JSON format I got while I added a file and some other things to MongoDB and I want to get this id separately to save the file to another folder.
Can anyone please explain this?
I believe this question was answered by Vikas in this thread How to get value from specific key in NodeJS JSON [duplicate]
Edited The Answer. Now Its working for above object in question
You can use following function to access the keys of JSON. I have
returned 'mm' key specifically.
function jsonParser(stringValue) {
var string = JSON.stringify(stringValue);
var objectValue = JSON.parse(string);
return objectValue['mm'];
}
if this is a JSON String, at first of all you have to parse it to object liertal, then you can access the specified property you mentioned, so you can do like the following:
function parseJsonString(jsonString) {
// first parse it to object
var obj = JSON.parse(jsonString);
// now access your object property/key
var id = obj.insertedId;
}
If your doing it on mongodb shell or Robo3T then, the line of code would be:
db.collection_name.find({},{insertedId:1})
This is related to projection. 1 represents you want to display it as your output.
In your case as you want only the value of your object id to get displayed, you have to use .valueOf() ; that is ObjectId().valueOf().
-> insertedId: new ObjectId("612d9d08db9c20f031033478")
-> ObjectId("612d9d08db9c20f031033478").valueOf()
->
-> 612d9d08db9c20f031033478
Your can refer this : https://docs.mongodb.com/manual/reference/method/ObjectId.valueOf/ site for your reference.
So you can use this .valueOf() in your code and get the id saved in the document
I have the following url and a parameter :
number:200
"http://localhost:8000/textpath"
I want the path to be like this:
"http://localhost:8000/textpath/200"
How do I do that using Reactjs?I want to use the appended url in fetch method as follows:
fetch("http://localhost:8000/textpath/200")
A simple append did the work.Doesn't have to complicate
fetch("http://localhost:8000/textpath/"+number)
Try using Template literals.
const url = `http://localhost:8000/textpath/${number}`
fetch(url);
where number = 200
Refer here for more information
I get this URL https://www.ikinciyeni.com/giris?ReturnUrl=/konsinye-hesap-sonuc&tempId=8642aa95-ebf9-4d00-8fd5-6d6c2f6023b2 I want to be able to get everything after tempId including tempId. How do I do that?
I have tried this:
var pattern = "tempId=";
var str2 = response.url().substr(response.url().indexOf(pattern), pattern.length);
console.log(str2);
How do I get to the end of string rather than just pattern's length
You can use the URL class to parse the url string, and then access the search parameters by name, for example:
// Create URL object from URL string.
const myUrl = new URL('https://www.ikinciyeni.com/giris?ReturnUrl=/konsinye-hesap-sonuc&tempId=8642aa95-ebf9-4d00-8fd5-6d6c2f6023b2');
// Get tempId parameter
console.log("tempId:", myUrl.searchParams.get("tempId"));
My express server handle a post request which receives a body containing the next JSON object:
"QnsAns": {
}
How can I validate that object QnsAns HAS nested object? It should look like:
"QnsAns": {
"Q1": "A1",
"Q2": "A2",
"Q3": "A3",
"Q4": "A4",
"Q5": "A5"
}
Using express validator, I tried using '*' wildcard, optional(), exist() but all result with no success
Thanks!
To check if "QnsAns" constains "Q1" you can use hasOwnProperty
console.log(QnsAns.hasOwnProperty('Q1'));
To check type of Q1, you can use typeOf
console.log(typeof(Q1);
If you have to use this multiple times, try out Joi Validations
The question is not very clear. Is this object in JSON format? Can you please post some more details to it so that it will help.
Now, as a simple solution you can do something like this, assuming the object is stored in a variable data.
function checkIfNestedObjectIsEmpty() {
let data = <Your Object>
return (Object.entries(data[Object.keys(data)]).length === 0)
}
Within a collection document there is a 'sub' property that is an object containing 1 or more properties. The collection looks like: (I've removes extraneous other properties)
"properties": {
"source": {
"a/name": 12837,
"a/different/name": 76129
}
}
I need to do a find on the collection where a/name and a/different/name are variables.
The variables have embedded front slashes because they are mqtt topic names, in case you wonder.
(node.js)
I've tried:
collection.find({'properties.source[variable containing name]': {$exists: true}}).toArray(function...
Doesn't work, nothing returned
I've also tried setting a query string as in:
var q = util.format('properties.source.%s', variable containing name);
collection.find({q: {$exists: true}}).toArray(function...
Fails with query error
I could replace the front slashes with some other character if they are the problem but I suspect they are ok. I have tried escaping the front slashes and it makes no difference.
Any suggestions?
You can't directly use a variable as an object key, so you need to change it to something like:
var name = 'a/name';
var query = {};
query['properties.source.' + name] = {$exists: true};
collection.find(query).toArray(function...
As you have properties an object and source is also an object, You should use $exist in a query like following :
db.collection.find({"properties.source.a/name":{$exists:true}})