How to do nested loops with object jSON data on nodejs - javascript

I'm new use nodejs and mongodb, now I build restful API use nodejs and mongodb. I want use response standard for my API with http://jsonapi.org standard.
I need a suggestion from advance how best way to do it, making my API response like the following JSON data:
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"links": {
"self": "http://example.com/users"
},
"data": [{
"type": "users",
"id": 5b647bb8998248235a0aab3c,
"attributes": {
"username": "luke",
"email": "luke#mail.com",
"password": "password",
"hashpassword":"",
"oldpassword":"$2a$10$eyt6YV6m2JJrebNxvS0iEuxMCubDXratNJ6/XK797IGvepXBdp9Yq",
"salt":"2q6eN9U0vWFBsIF1MtB5WrgPiB8pldTS",
"usertype":"bisnis",
"userstatus":"not aktif"
}
}, {
"type": "users",
"id": 5b647bdf998248235a0aab3d,
"attributes": {
"username": "ken",
"email": "ken#mail.com",
"password": "password",
"hashpassword":"",
"oldpassword":"$2a$10$eyt6YV6m2JJrebNxvS0iEuxMCubDXratNJ6/XK797IGvepXBdp9Yq",
"salt":"2q6eN9U0vWFBsIF1MtB5WrgPiB8pldTS",
"usertype":"bisnis",
"userstatus":"not aktif"
}
}]
}
I have problem with nest iteration when create output as above. This my code:
const UsersModel = mongoose.model('Users');
//...show list user
exports.listUsers = (req, res) => {
UsersModel.find({}, (err, result) => {
if (err) {
res.send({code: 400, failed: 'Error 400'});
}
res.json(result);
});
};
And this is my result JSON:
[
{
"type": "users",
"userstatus": "not aktif",
"_id": "5b647bb8998248235a0aab3c",
"username": "luke",
"email": "luke#mail.com",
"password": "password",
"usertype": "bisnis",
"hashpassword": "$2a$10$eyt6YV6m2JJrebNxvS0iEuxMCubDXratNJ6/XK797IGvepXBdp9Yq",
"salt": "2q6eN9U0vWFBsIF1MtB5WrgPiB8pldTS",
"__v": 0
},
{
"type": "users",
"userstatus": "tidak aktif",
"_id": "5b647bdf998248235a0aab3d",
"username": "ken",
"email": "ken#mail.com",
"password": "password",
"usertype": "personal",
"hashpassword": "$2a$10$hok988mszyIBiXVNjmfifOiPNzXkBRRRynXJS/0qCkvlaBOQs65MO",
"salt": "IiMvtVYVqTpZFXmYQIM4IlS6PJFVZ3kw",
"__v": 0
}
]
And this is my temporary code for my problem.
//...show list user
exports.listUsers = (req, res) => {
UsersModel.find({}, (err, result) => {
if (err) {
res.send({code: 400, failed: 'Error 400'});
}
let listData = [];
for (let key in result) {
let data = {};
let attr = {};
if (result.hasOwnProperty(key)) {
data.type = result[key].type;
data.id = result[key]._id;
for(let i in result[key]) {
if(result[key].hasOwnProperty(i)) {
attr.username = result[key].username;
attr.email = result[key].email;
attr.password = result[key].password;
attr.hashpassword = result[key].hashpassword;
attr.oldpassword = result[key].oldpassword;
attr.salt = result[key].salt;
attr.usertype = result[key].usertype;
attr.userstatus = result[key].userstatus;
}
}
data.attribute = attr;
listData.push(data);
}
}
let collections = {
"meta": {
"copyright": "Copyright 2018 Kotakku Studio and Lab",
"authors": [
"sw. saputra"
]
},
"link": {
"self": req.protocol + '://' + req.get('host') + req.originalUrl
},
"data": listData
}
res.json(collections);
});
};
Please give me suggestion the elegant and the best way to solve my problem if my temporary code is not correct.
Thanks advance.

You need to create a proper mongoose schema according to your requirement and map the res to the schema.
//Schema for users
var UsersSchema = mongoose.Schema({
links: {
self: String
},
data:[{
type: String,
attributes: {
username: String,
.
.
.
}
}]
})
id for each document in an array will be created automatically by MongoDB.
For mongoose schema docs see http://mongoosejs.com/docs/guide.html
You can also check this simple TODO API for reference https://github.com/mkujaggi/node-course-todo-api

In this condition's statement:
if(result[key].hasOwnProperty(i)) {
You should be accessing the i of result[key]:
if(result[key].hasOwnProperty(i)) {
attr.username = result[key][i].username;
attr.email = result[key][i].email;
attr.password = result[key][i].password;
attr.hashpassword = result[key][i].hashpassword;
attr.oldpassword = result[key][i].oldpassword;
attr.salt = result[key][i].salt;
attr.usertype = result[key][i].usertype;
attr.userstatus = result[key][i].userstatus;
}
And you don't need all those .hasOwnProperty checks unless you're modifying native prototypes, which you probably shouldn't. And especially for the Array, it's not necessary if you'd just loop properly with a for instead of a for-in.
exports.listUsers = (req, res) => {
UsersModel.find({}, (err, result) => {
if (err) {
res.send({
code: 400,
failed: 'Error 400'
});
}
let listData = [];
let data = {};
let attr = {};
data.type = result[key].type;
data.id = result[key]._id;
for (let i = 0; i < result[key].length; i++) {
attr.username = result[key][i].username;
attr.email = result[key][i].email;
attr.password = result[key][i].password;
attr.hashpassword = result[key][i].hashpassword;
attr.oldpassword = result[key][i].oldpassword;
attr.salt = result[key][i].salt;
attr.usertype = result[key][i].usertype;
attr.userstatus = result[key][i].userstatus;
}
data.attribute = attr;
listData.push(data);
}
}
let collections = {
"meta": {
"copyright": "Copyright 2018 Kotakku Studio and Lab",
"authors": [
"sw. saputra"
]
},
"link": {
"self": req.protocol + '://' + req.get('host') + req.originalUrl
},
"data": listData
}
res.json(collections);
});
};

I would do something like this with iterating over the keys (this will work dynamically that is even if we don't know key names)
var test=[
{
"type": "users",
"userstatus": "not aktif",
"_id": "5b647bb8998248235a0aab3c",
"username": "luke",
"email": "luke#mail.com",
"password": "password",
"usertype": "bisnis",
"hashpassword": "$2a$10$eyt6YV6m2JJrebNxvS0iEuxMCubDXratNJ6/XK797IGvepXBdp9Yq",
"salt": "2q6eN9U0vWFBsIF1MtB5WrgPiB8pldTS",
"__v": 0
},
{
"type": "users",
"userstatus": "tidak aktif",
"_id": "5b647bdf998248235a0aab3d",
"username": "ken",
"email": "ken#mail.com",
"password": "password",
"usertype": "personal",
"hashpassword": "$2a$10$hok988mszyIBiXVNjmfifOiPNzXkBRRRynXJS/0qCkvlaBOQs65MO",
"salt": "IiMvtVYVqTpZFXmYQIM4IlS6PJFVZ3kw",
"__v": 0
}
]
var listData=[]
test.forEach((obj)=>{
var tempObj={attributes:{}}; //initialize tempObj
Object.keys(obj).forEach((key)=>{
if(key=="_id" || key=="type"){
tempObj[key]=obj[key];
}else{
tempObj.attributes[key]=obj[key];
}
})
listData.push(tempObj);
})
let collections = {
"meta": {
"copyright": "Copyright 2018 Kotakku Studio and Lab",
"authors": [
"sw. saputra"
]
},
"link": {
"self": 'test url'
},
"data": listData
}
console.log(collections)

Related

Update nested JSONB in postgresql using sequelize

I am using NodeJS to update a nested info in the database but I can't seem to figure this out.
Data in database
{
"id": 1,
"data": {
"__v": 0,
"_id": "5887e1d85c873e0011036889",
"text": "you have to change this text",
"type": "cat",
"used": true,
"user": "5a9ac18c7478810ea6c06381",
"source": "user",
"status": {
"feedback": "",
"verified": true,
"sentCount": 1
},
My code to update:
UpdateFacts: async function (req, res, next) {
const {text} = req.body
const {id} = req.params
if(!id){
return res.status(400).send({message:'please provide id'})
}
if(!Object.keys(req.body).length){
return res.status(400).send({message:'please provide text'})
}
const checkIfFactExist = await Facts.findOne({
where: {
id
}
})
if(!checkIfFactExist){
return res.status(404).send({message:'this id does not exist'})
}
try {
if(text){
checkIfFactExist.data.text = text
}
checkIfFactExist.save()
return res.status(200).send({message:'updated'})
} catch (error) {
return res.status(500).send(error.message)
}
Data is the column and text is the field am trying to change but it's not working.

Update multiple or single object in an array with specified data from request

I am not great with MongoDB's advanced techniques.
My record in the MongoDB collection:
{
"_id": ObjectId("1"),
"manager": ObjectId("12345"),
"code": "PS",
"title": "Performance System",
"users": [
{
"_user": ObjectId("1"),
"role": "Member",
},
{
"_user": ObjectId("2"),
"role": "Member",
},
{
"_user": ObjectId("3"),
"role": "Member",
}
],
}
Node.js / ExpressJS
I created API to update the array like below but did not work.
const updateProjectMember = asyncHandler(async (req, res) => {
const { userID, role } = req.body.userData;
try {
const project = await Project.updateMany(
{ _id: req.params.projectID },
{ $set: { "users.$[selectedUser].role": role } },
{ arrayFilters: { "selectedUser._user": { $in: userID } } }
);
res.status(200).json(project);
} catch (error) {
res.status(400);
throw new Error(error);
}
I use the API parameter to get the project ID. Here is the request body data:
{
userID : ["2","3"];
role: "Admin"
}
So the API will get an array of userID to match and set all "role" fields to "Admin" to all matched.
I wanted the data to be like this:
{
"_id": ObjectId("1"),
"manager": ObjectId("12345"),
"code": "PS",
"title": "Performance System",
"users": [
{
"_user": ObjectId("1"),
"role": "Member",
},
{
"_user": ObjectId("2"),
"role": "Admin",
},
{
"_user": ObjectId("3"),
"role": "Admin",
}
],
}
Am I doing the right practice? If it is bad practice, what is the best way to solve this?
The query is fine. Just make sure that you pass the value with the exact type as in the MongoDB document.
var mongoose = require('mongoose');
const updateProjectMember = asyncHandler(async (req, res) => {
const { userID, role } = req.body.userData;
userID = userID.map(x => mongoose.Types.ObjectId(x));
try {
const project = await Project.updateMany(
{ _id: mongoose.Types.ObjectId(req.params.projectID) },
{ $set: { "users.$[selectedUser].role": role } },
{ arrayFilters: { "selectedUser._user": { $in: userID } } }
);
res.status(200).json(project);
} catch (error) {
res.status(400);
throw new Error(error);
}
}

Associate two keys in an object JavaScript

So I have this object that has two keys, clinics and invitations. I want to associate the clinics with the invitations taking into account the clinicId:
const upcomingClinics = {
"clinics": {
"a0CW000000271LuMAI": {
"id": "a0CW000000271LuMAI",
"contact": {
"name": null,
"phone": null,
"email": null
},
"shifts": {
"teamLeads": 1,
"healthTechs": 1
}
},
"a0CW00000026gikMAA": {
"id": "a0CW00000026gikMAA",
"contact": {
"name": null,
"phone": null,
"email": null
},
"shifts": {
"teamLeads": 1,
"healthTechs": 4
}
}
},
"invitations": {
"56392": {
"id": "56392",
"clinicId": "a0CW00000026gikMAA"
},
"56393": {
"id": "56393",
"clinicId": "a0CW00000026gikMAA"
},
"56402": {
"id": "56402",
"clinicId": "a0CW00000026gikMAA"
},
"56427": {
"id": "56427",
"clinicId": "a0CW000000271LuMAI"
},
"56428": {
"id": "56428",
"clinicId": "a0CW000000271LuMAI"
}
}
}
The keys of the clinics object always match the IDs. Basically I want this object to look like this, because they have in common the same clinicId, how can I do this? Inserting a new key invitations to the clinics object?:
const upcomingClinics = {
"clinics": {
"a0CW000000271LuMAI": {
"id": "a0CW000000271LuMAI",
"contact": {
"name": null,
"phone": null,
"email": null
},
"shifts": {
"teamLeads": 1,
"healthTechs": 1
}
"invitations": {
"56427": {
"id": "56427",
"clinicId": "a0CW000000271LuMAI"
},
"56428": {
"id": "56428",
"clinicId": "a0CW000000271LuMAI"
}
}
},
"a0CW00000026gikMAA": {
"id": "a0CW00000026gikMAA",
"contact": {
"name": null,
"phone": null,
"email": null
},
"shifts": {
"teamLeads": 1,
"healthTechs": 4
}
"invitations": {
"56392": {
"id": "56392",
"clinicId": "a0CW00000026gikMAA"
},
"56393": {
"id": "56393",
"clinicId": "a0CW00000026gikMAA"
},
"56402": {
"id": "56402",
"clinicId": "a0CW00000026gikMAA"
},
}
}
},
}
Thanks!
i think this is what you are looking for https://jsfiddle.net/q4rt6zad/10/
Object.getOwnPropertyNames(upcomingClinics.clinics).forEach((clinicId) => {
upcomingClinics.clinics[clinicId].invitations = {};
Object.getOwnPropertyNames(upcomingClinics.invitations).forEach((id) => {
const invite = upcomingClinics.invitations[id];
if (invite.clinicId === clinicId) {
upcomingClinics.clinics[clinicId].invitations[id] = invite;
}
});
});
delete upcomingClinics.invitations;
Just loop the invitations object, and for each invitation check if its clinic is already included in upcomingClinics object, if so then just add this invitation to its invitations object, otherwise, create a new clinic record in upcomingClinics then insert the current invitation to its invitations object:
let result = Object.keys(upcomingClinics.invitations).reduce(function(result, invitationId) { // for each invitationId in upcomingClinics.invitations object
let invitation = upcomingClinics.invitations[invitationId]; // get the current invitation object
let clinicId = invitation.clinicId; // get its clinicId
if(!result[clinicId]) { // if there is no record of this clinic in the result object
result[clinicId] = Object.create(upcomingClinics.clinics[clinicId]); // create one by cloning the clinic object from upcomingClinics.clinics
result[clinicId].invitations = {}; // create an object that will hold its invitations
}
result[clinicId].invitations[invitationId] = invitation; // add the current invitation to its corresponding clinic object
return result;
}, {});
Example:
const upcomingClinics = {"clinics":{"a0CW000000271LuMAI":{"id":"a0CW000000271LuMAI","contact":{"name":null,"phone":null,"email":null},"shifts":{"teamLeads":1,"healthTechs":1}},"a0CW00000026gikMAA":{"id":"a0CW00000026gikMAA","contact":{"name":null,"phone":null,"email":null},"shifts":{"teamLeads":1,"healthTechs":4}}},"invitations":{"56392":{"id":"56392","clinicId":"a0CW00000026gikMAA"},"56393":{"id":"56393","clinicId":"a0CW00000026gikMAA"},"56402":{"id":"56402","clinicId":"a0CW00000026gikMAA"},"56427":{"id":"56427","clinicId":"a0CW000000271LuMAI"},"56428":{"id":"56428","clinicId":"a0CW000000271LuMAI"}}};
let result = Object.keys(upcomingClinics.invitations).reduce(function(result, invitationId) {
let invitation = upcomingClinics.invitations[invitationId];
let clinicId = invitation.clinicId;
if(!result[clinicId]) {
result[clinicId] = Object.create(upcomingClinics.clinics[clinicId]);
result[clinicId].invitations = {};
}
result[clinicId].invitations[invitationId] = invitation;
return result;
}, {});
console.log(result);
const clinics = {};
for (let clinicId in upcomingClinics.clinics) {
clinics[clinicId] = upcomingClinics.clinics[clinicId];
clinics[clinicId].invitations = {};
for (let invitId in upcomingClinics.invitations) {
const invitation = upcomingClinics.invitations[invitId];
if (invitation.clinicId === clinicId) {
clinics[clinicId].invitations[invitId] = invitation;
}
}
}
https://jsfiddle.net/bg6srahq/

NodeJs: Data Transformers like in Laravel PHP Framework

I've created multiple REST API projects using the Laravel framework and basing my code structure on the Laracasts tutorial. However we are deciding to move some projects using NodeJs as a backend. I'm beginning to learn node and I'm trying to replicate it in Node. I was able to do it for a singe object response but for multiple objects I can't seem to make it work.
Here is my controller:
index(req,res) {
User
.findAll()
.then(function(users){
res.json(api.respond(transfomer.transformCollection(users)));
})
.catch(function(error){
res.json(api.respondWithError('users not found',error));
});
}
api controller:
module.exports = {
// response w/o error
respond: function(data,msg,status) {
if (msg == null) {
return {
'status': status || true,
'data': data
};
} else {
return {
'status': true,
'message': msg,
'data': data
};
}
},
// response with error
respondWithError: function(msg,error) {
var self = this;
var status = false;
var data = {
'error': error
};
return this.respond(data,msg,status);
},
};
transformer.js
module.exports = {
// single transformation
transform (user) {
return {
'id' : user.id,
'username': user.username,
'firstname': user.firstname,
'lastname': user.lastname,
'address': user.address,
'phone': user.phone,
'mobile': user.mobile,
'status': user.status
};
},
//
transformCollection(users) {
var self = this;
var data = [];
for (var i = 0; i <= users.length; i++) {
data.push(this.transform(users[i]));
}
return data;
}
};
sample output
{
"status": true,
"data": [
{
"id": 1,
"username": "b#email.com",
"firstname": "Jon",
"lastname": "Doe",
"address": "Homes",
"phone": "+966501212121",
"mobile": "+966501212121",
"status": "NOT VERIFIED"
},
{
"id": 1,
"username": "b#email.com",
"firstname": "Jon",
"lastname": "Doe",
"address": "Homes",
"phone": "+966501212121",
"mobile": "+966501212121",
"status": "NOT VERIFIED"
},
{
"id": 1,
"username": "b#email.com",
"firstname": "Jon",
"lastname": "Doe",
"address": "Homes",
"phone": "+966501212121",
"mobile": "+966501212121",
"status": "NOT VERIFIED"
},
{
"id": 1,
"username": "b#email.com",
"firstname": "Jon",
"lastname": "Doe",
"address": "Homes",
"phone": "+966501212121",
"mobile": "+966501212121",
"status": "NOT VERIFIED"
},
]
}
Sorry for asking this as I'm a bit newb with node. Is it possible to achieve that output as I tried different ways but Im still getting errors. Btw I'm using sequelize for the database.
Thanks.
You can use this:
const options = {
raw: true,
attributes: ['id', 'name', 'code', 'createdAt','updatedAt']
};
country.findAndCountAll(options).then(querySnapshot => {
const total = querySnapshot.count;
resolve({
docs: querySnapshot.rows,
total: total
})
}).catch((err) => {
reject(err)
});
I've found the answer to my question since sequelize is returning the results as an object with additional properties aside from the database results I had to modify the controller to set and convert the results to raw in order for me to get the array of objects from the query results from the database.
index(req,res) {
User
.findAll({ raw: true }) // added "raw: true"
.then(function(users){
res.json(api.respond(transfomer.transformCollection(users)));
})
.catch(function(error){
res.json(api.respondWithError('users not found',error));
});
},
This will return the array of objects from the database and from there the data transformer is working properly. Thank you for all the help.

node js mongoose find data from array in collection

I am working with node.js and mongoose I am stuck in a problem. My users collection looks like.
{
"_id": ObjectId("564b6deec50de8d827c0a51a"),
"email": "ak#gmail.com",
"ngos": [
{
"_id": ObjectId("564b7527ecc479e4259edff7"),
"name": "Children trust",
},
{
"_id": ObjectId("564b79e0ecc479e4259edff8"),
"name": "Charity Two",
"location": "Australia"
}
]
}
{
"_id": ObjectId("564e0a18c8cd4b5420a9250c"),
"email": "some#gsomel.com",
"ngos": [
{
"_id": ObjectId("564e0b3bc8cd4b5420a92510"),
"name": "Charity Two",
"location": "US"
}
]
}
I want to find all the ngos whose name is like Charity so it should return me.
{
"_id": ObjectId("564e0b3bc8cd4b5420a92510"),
"name": "Charity Two",
"location": "US"
}
{
"_id": ObjectId("564e0b3bc8cd4b5420a92510"),
"name": "Charity Two",
"location": "Australia"
}
I tried
User.find({"ngos.name": new RegExp(name, 'i')}, function(err, user) {
if (err) return next(err);
res.json(user);
});
It gave me both users with all the data as I am returning the user but if I change res.json(user); to res.json(user.ngos); I am not getting any response.
How can I retreive those particular ngos whose name matches?
Thanks
Use regex filtering on your final result array as follows:
var rgx = new RegExp(name, 'i');
User.find({"ngos.name": rgx})
.lean()
.exec(function(err, users) {
if (err) return next(err);
var result = users.map(function (n){
return n.ngos.filter(function(val){return rgx.test(val.name);});
})
console.log(JSON.stringify(result, undefined, 4));
res.json(result);
});
Check the demo below.
var cursor = [
{
"ngos": [
{
"_id": "564b7527ecc479e4259edff7",
"name": "Children trust",
},
{
"_id": "564b79e0ecc479e4259edff8",
"name": "Charity One",
"location": "Australia"
}
]
},
{
"ngos": [
{
"_id": "564e0b3bc8cd4b5420a92510",
"name": "Charity Two",
"location": "US"
}
]
}
];
var rgx = new RegExp('Charity', 'i');
var result = cursor.map(function (n){
return n.ngos.filter(function(val){return rgx.test(val.name);});
})
pre.innerHTML = "result: " + JSON.stringify(result, null, 4);
<pre id="pre"></pre>
Hope this helps,
User.find({"ngos.name": new Regex(name, 'i')},{'ngos':1}).exec(function(err,data){
if (err) throw(err);
res.json(data);
});
just try this way in mongoose
you getting res is Array so use "forEach"
User.find({'ngos.name':new RegExp('name', 'i')}, {'ngos':1},function(err,users){
users.forEach( function(user) {
user.ngos.forEach( function(nom) {
console.log( nom );
})
} );
})
just try this way in mongodb
db.user.find({'ngos.name':new RegExp('name', 'i')},{'ngos':1}).forEach( function(user) {
user.ngos.forEach( function(nom) {
print( nom );
})
} )
I think this help to u !

Categories

Resources