POST relation in strapi - node.js

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
}}

Related

POST database relation in strapi

I have a strapi project and A flights has somr relations like drones, pilots. For example if we using postman to get a flight i send :
{
"data" : {
"Duration" : "10",
"Address" : "Address",
"account": 1,
"drone": "1",
"pilot": 1,
"User": 1,
"zone": 1,
}
}
the response is :
{
"id": 25,
"Duration" : "10",
"Address" : "Address",
"account": 1,
"drone": "1",
"pilot": 1,
"User": {
"id": 1,
"username": "AhmedMedhat",
"email": "ahmedmedhat1231#gmail.com",
"provider": "local",
"password": "$2a$10$8tkwcp73AbE0aylQyOsIdefgSiuaoOcPJzbZWxk9W2IN85SghOIjO",
"resetPasswordToken": null,
"confirmationToken": null,
"confirmed": false,
"blocked": false,
"createdAt": "2022-11-03T06:32:57.225Z",
"updatedAt": "2022-11-03T06:35:05.902Z"
},
"zone": 1,
}
only the user is populated successfully but the other relations always return null ..
maybe bec user is plugin and others is api i don't know, it works in the strapi built in routes but my customized one doesn't work .. can anyone help ??

How do i get data from user table in mongos node js by relation id

I have a one order table
Mongo db version is 3.0.15
Order table have this columns
{
"status": "new",
"_id": "5fd320a8b3d5133c8bf00c4a",
"categoryId": "5fb2e20f5ea2aa2d15cca1ef",
"specialization": "Tesst",
"requestedBy": "5fd232ecd7ba652c3a85f818",
"vendorId": "5fa908773410d8591aa9f550",
"updated_at": 1607671976182,
"created_at": 1607671976182,
},
requested-by belongs to second table users
I want to fetch user details from user table username user-address
in user table i have this columns
{
"_id": "5fd232ecd7ba652c3a85f818",
"phone": "1234567890",
"otp" : "123456",
"progress_pictures": [],
"createdAt": "2020-12-10T14:38:36.045Z",
"updatedAt": "2020-12-10T14:39:35.127Z",
}
How can I get data anyone know ?
Order.find(
{
status : status
},
{
}
).exec();
This is my order invite query this is working find get only order table data
i found solution using populate
Order.find(
{
status : status
},
{
}
).populate([
{
path: 'requestedBy', // in order table user ref id
select : 'name phone user_address'
},{
path : 'vendorId', // in order table vendor ref id
select : 'vendorName vendorMobile vendorAddress vendorBusinessName'
}]).exec();
----------------------- RESPONSE -----------------------
{
"status": "new",
"_id": "5fd320a8b3d5133c8bf00c4a",
"categoryId": "5fb2e20f5ea2aa2d15cca1ef",
"specialization": "Tesst",
"requestedBy": {
"user_address": [],
"name" : "nikhil",
"_id": "5fd232ecd7ba652c3a85f818",
"phone": "1234567890"
},
"vendorId": {
"_id": "5fa908773410d8591aa9f550",
"vendorMobile": "1234567890",
"vendorAddress" : "demo address",
"vendorBusinessName" : "testing hub",
"vendorName": "KL"
},
"updated_at": 1607671976182,
"created_at": 1607671976182,
},

NodeJs and Postman

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.

get stream-io response data is changed to string from json object

Payload sending to getStream.io server
{
"feed": {
"actor": 1986,
"created_at": 1455690430,
"foreign_id": "1986",
"object": {
"full_name": "Harsewak Singh",
"login": "harsewak",
"pic_url": "https:\/\/qbapi.dekhdekh.com\/blobs\/b6e113a29bb34905a14a520917f6da0a00",
"user": 1986
},
"post": {
"created_at": 1455690430,
"message": "Hey! How's everyone?",
"type": "text"
},
"to": ["flat:global"],
"verb": "post"
},
"feed_type": "flat",
"user_id": 1986
}
But when data is returned value of object isn't same as sent
It's as follows
"object":
"{u'login': u'harsewak', u'pic_url': u'https://qbapi.dekhdekh.com/blobs/b6e113a29bb34905a14a520917f6da0a00', u'full_name': u'Harsewak Singh', u'user': 1986}"
Q. Why u' is added in response after replaceing double quotes ("login")?
Sending nested data in the object field is currently not supported by Stream's APIs.
if you are using Laravel and Eloquent, you should integrate your models like documented here: https://github.com/GetStream/stream-laravel#eloquent-integration This way you will get serialization and deserialization working for free.

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