NodeJs and Postman - node.js

I have 2 databases; First one is Users database where a registered user is an object with the 4 following keys (name, password, email and an array called "cats"). My next database is called Cats (again an object with 4 keys).
Now what I want to do is to update the User.cats each time the specific user uploads a cat in the Cats database. I have written all the requests for both databases (getUsers/getCats, postUser/postCat, putUser/putCat, patchUser/patchCat, deleteUser/deleteCat, getUserByID/GetCatById, getUserCount/getCatsCount);
I don't know how to access the users database so that User.cats gets updated whenever the user registers a new cat on his account.
These are the users:
{
"result": [
{
"name": "Razvan",
"email": "razvan#gmail.com",
"password": "Tricul",
"cats": []
},
{
"name": "mimisor",
"email": "mimi#hotmail.com",
"password": "MiMi",
"cats": []
},
{
"name": "Omar",
"email": "omar#gmail.com",
"password": "tataItz",
"cats": []
},
{
"name": "Dana",
"email": "danaraluca#gmail.com,",
"password": "MamaItz",
"cats": []
}
]
}
and these are the cats:
"result": [
{
"name": "Cocorico",
"age": "15 ani",
"breed": "Black Cat",
"owner": "UserId"
},
{
"name": "Mimi",
"age": "3 ani",
"breed": "Egyptian Mau",
"owner": "UserId"
},
{
"name": "Blondsky",
"age": "10 ani",
"breed": "Ginger Boy",
"owner": "UserId"
},
{
"name": "Billy",
"age": "1 ani",
"breed": "Aarabian Mau",
"owner": "UserId"
}
]
}

Basically you have two collections relationship.
I assume that you are using MongoDB. You can read about Mongoose to generate the component user and cat and use populate (I don't remember is MongoDB has something native to do that)
https://vegibit.com/mongoose-relationships-tutorial/
Other option for any DB, in your function you can read the users and do an iteration where for each user, you read from DB the cats that you have for that user and complete the field cats.

Related

I have some user information in the JSON file. I want to update multiple users' information by using the fs module in nodeJS

[
{
"id": 1,
"gender": "male",
"name": "Harry",
"contact": "01765489231",
"address": "park view road, Dhaka",
"photoUrl": "https://images.unsplash.com/flagged/photo-1570612861542-284f4c12e75f?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8M3x8cGVyc29ufGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60"
},
{
"id": 2,
"gender": "male",
"name": "Xavier",
"contact": "01648935261",
"address": "Ring house, Bogra",
"photoUrl": "https://images.unsplash.com/photo-1547425260-76bcadfb4f2c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8cGVyc29ufGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60"
},
{
"id": 3,
"gender": "female",
"name": "Anyy",
"contact": "01554783921",
"address": "321 street, Comilla",
"photoUrl": "https://images.unsplash.com/photo-1499952127939-9bbf5af6c51c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTJ8fHBlcnNvbnxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=500&q=60"
},
{
"id": 4,
"gender": "female",
"name": "Ember",
"contact": "01812398654",
"address": "Golden street, Rajshahi",
"photoUrl": "https://images.unsplash.com/photo-1593104547489-5cfb3839a3b5?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MzB8fHBlcnNvbnxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=500&q=60"
},
{
"id": 5,
"gender": "male",
"name": "Gerrad",
"contact": "01946378254",
"address": "Ali complex, Khulna",
"photoUrl": "https://images.unsplash.com/photo-1568602471122-7832951cc4c5?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NDV8fHBlcnNvbnxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=500&q=60"
}
]
After hitting '/user/bulk-update' route I have to do the following:
Take an array of user ids and assign it to the body.
Update multiple users' information in this .json file
Please don't just ask for code next time.
const data = JSON.parse(fs.readFileSync('myjson.json'));
const {ids} = req.body;
data.forEach(entry => {
// Do your things here
})
Assuming you have a server already set up.

POST relation in strapi

I have a strapi project and A single users have relation a single profile. For example if we using postman to get a single user it have response
[
{
"id": 45,
"username": "test",
"email": "test#gmail.com",
"provider": "local",
"confirmed": true,
"blocked": false,
"createdAt": "2022-07-18T08:50:43.642Z",
"updatedAt": "2022-07-18T08:50:43.642Z",
"profile": null
}
]
As you can see a user have a profile but have a null value. So when I tried to post a profile in strapi api http://localhost:1337/api/profiles?populate=* with body of json like this
{
"data" : {
"name": "fgh",
"address": "jksdjkdjs",
"phone": "345345",
"email" : "test#gmail.com" /// I tried to add this but the response is 500 internal server error
}}
It worked and status 200 ok but it didn't get the relation of the email from user
{
"data": {
"id": 12,
"attributes": {
"name": "fghdff",
"address": "jksdjkdjs",
"phone": 34534235,
"createdAt": "2022-08-05T13:48:29.548Z",
"updatedAt": "2022-08-05T13:48:29.548Z",
"publishedAt": "2022-08-05T13:48:29.547Z",
"email": {
"data": null
}
}
},
"meta": {}}
Any Idea how to post a relation in strapi? I tried to read docs or strapi forum but no one counter this problem
For someone facing how to POST relation in strapi we can use the ID of the User. For example to post and connect in my question is
{
"data" : {
"name": "dayee",
"address": "jksdjkdjs",
"phone": "34534235",
"email" : 77 // Post relation using ID
}}

How to query all documents where array field have at least one object-element with property equal 'X'?

I have some MongoDB documents like that:
{
"group": "P32666",
"order": [{
"_id": {
"$oid": "5e8e9b40e7999f6b90fd88bf"
},
"name": "Dmitriy A",
"login": "example",
"password": "example",
"email": "example",
"level": "user",
"uuid": "b6a19744-bb20-4d39-9e1e-0ca5b464f890"
}, {
"_id": {
"$oid": "5e8ea03f5a21c26b90983de4"
},
"name": "Dmitriy B",
"login": "example",
"password": "example",
"email": "example",
"level": "user",
"uuid": "556924c3-605c-44cc-8a26-d32f58222e89"
}, {
"_id": {
"$oid": "5e8ea0645a21c26b90983de5"
},
"name": "Dmitriy C",
"login": "example",
"password": "example",
"email": "example",
"level": "user",
"uuid": "aef00707-ef00-4ce9-918b-5cef17e7280b"
}]}
I'm working with Mongo in Mongoose and can't understand how to query all documents(like above) where field(array) "order" has at least one object within where field, for example, "login" is queal to "example". How can I do this?
I tried something like this:
export async function getQueues(request: Request, response: Response) {
const returningQueues = await queues.find({order: [login: request.params.login]});
response.json(returningQueues);
But TypeScript error (56 missing properties(mostly internal Mongoose's) of type "User", which I store within an array) says that's I am wrong in my thoughts.
If you just have one condition on the array field, you can simply do:
queues.find({"order.login": request.params.login})
But if you have multiple conditions, you can use $elemMatch:
queues.find({
order: { $elemMatch: { login: request.params.login, name: request.params.name } }
})

Seed mongoDB with mongoose-seed

i am using mongoose-seed module to seed my database with some initial data, data is in json file and is not seeded in order from file
[{
"model": "User",
"documents": [
{
"name": "User1",
"email": "user1#gmail.com"
},
{
"name": "User2",
"email": "user2#gmail.com"
},
{
"name": "User3",
"email": "user3#gmail.com"
}
]
}]
In my db data is seeded in random order, not like user1, user2, user3.
Use Seedgoose, the ultimate seeder with smart reference support.

How to Write search Query for Couchdb in views where the search object is in array?

I have a documents having the following data.
Document 1.
{
"_id": "7d7db310ff3acc857c7f301f67979de5",
"_rev": "1-3ed97634540c35292155ad40b99cafc1",
"categories": [
"Computer",
"Mobile",
"Automobile",
"Plumbing"
],
"location": [
"19.374636239520235",
"72.82339096069336"
],
"gender": "male",
"username": "ayushcshah",
"password": "sokdncx76483ynxwhakdkxfka37",
"email": "ayushcshah#gmail.com"
}
Document 2.
{
"_id": "8c499253bce69b992ebe795906fac3d3",
"_rev": "1-ce394be6ab3c79111344f026e1a3adcf",
"categories": [
"Automobile"
],
"location": [
"19.387757921807914",
"72.82626360654831"
],
"gender": "male",
"username": "smithabc",
"password": "adsadgq36eygdas2ygduabhs",
"email": "smithabc#gmail.com"
}
I need a VIEW where I would send key as any string and it would find a document where categories array contain the search string and it would send me related user's email address in value.
Example:
If I send key as "Mobile" the I would get respected username as value "ayushcshah#gmail.com"
key":"Mobile","value":"ayushcshah#gmail.com"
key":"Computer","value":"ayushcshah#gmail.com"
key":"Automobile","value":"ayushcshah#gmail.com"
key":"Automobile","value":"smithabc#gmail.com"
key":"Plumbing","value":"ayushcshah#gmail.com"
Following map function should be enough:
function (doc) {
if (!doc.categories) return;
for (var c in doc.categories) {
emit(doc.categories[c], doc.email);
}
}

Resources