Update nested JSONB in postgresql using sequelize - javascript

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.

Related

Replace a value in a json file with value from another file using fs

I want to read the values of key, access, path, bucket and bucketPath and use them in the JSON file test.json.
I have a function that reads the content of configuration.js and attempts to write to test.json. Currently, I am able to write the values of bucket.I get the changed/new values and lines of null for the rest of the json.
I want to always return the new values and the other objects in the file. Also, in cases where bucket already has a value, I want it replaced by whatever is read from configuration.json
How can I fix this, and how can i change the values for the rest access, key, path and bucketpath?
index.js
const fs = require("fs").promises;
async function readJSON(filePath, values) {
const data = await fs.readFile(filePath);
try {
return JSON.parse(data);
} catch (err) {
console.log(err);
}
}
(async() => {
const credentials = await readJSON("./configuration.json");
const path = credentials.path;
const bucket = credentials.bucket;
const access = credentials.access;
const key = credentials.key;
const bucketPath = credentials.bucketPath;
const data = await jsonReader("./test.json");
const finalJSON = data.data ? .map((x) => {
if (x.type == "s3 credentials") return { ...x, bucket };
});
await fs.writeFile(
"./test.json",
JSON.stringify({
data: finalJSON
})
);
})();
test.json
{
"label": "storage record",
"data": [{
"id": "8902uw",
"type": "config",
"values": {
"access": "$access",
"key": "$key"
}
},
{
"id": "893wh002jei",
"type": "s3 credentials",
"bucket": ""
},
{
"id": "90yueps",
"type": "upload",
"input": "localhost: `$path`"
},
{
"id": "9028901",
"type": "change",
"name": "Adjust data",
"measure": [{
"t": "setter"
},
{
"p": "filename",
"to": "$join([\"$bucketPath\", data])"
}
],
"fixed": ""
}
]
}
configuration.json
{
"key": "880082",
"access": "793082",
"path": "/store",
"bucket": "testBucket",
"bucketPath": "/record"
}
Currently, when I run this, I get:
{
null,
"data": [{
null,
null,
null,
null
{
"id": "893wh002jei",
"type": "s3 credentials",
"bucket": ""
},
{
null,
null,
null
]
}
might this be a solution !
const fs = require('fs');
const fileName = './file.json';
const file = require(fileName);
file.key = "new value";
fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
if (err) return console.log(err);
console.log(JSON.stringify(file));
console.log('writing to ' + fileName);
});
[Updated answer]
From what you comment:
it's the same question. So when I run what I have, I get null for the other objects. I want test.json to remain the same just with updated values.
const testObj = await jsonReader("./test.json");
const finalJSON = {
...testObj,
data: testObj.data?.map((x) => {
if (x.type === 's3 credentials') {
return { ...x, bucket };
} else {
return x;
}
})
}
// which the code I gave above,
// the `finalJSON` now is the clone of the original from `test.json`
// plus updated values
await fs.writeFile(
"./test.json",
JSON.stringify(finalJSON)
);
[Original answer]
There is a problem with the function you pass to the map function.
The condition if without else.
I think you need else { return x; } to return original data if x.type is not what you expected.

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

How to create an order from a cart in Mongoose and delete cart

I am trying to:
a) Create an order from my current cart
b) Populate the response of the order with the selected object attributes in the "select"
c) Finally, I would like to delete the cart.
I have tried the below but here are my problems:
the result response does not come back populated even if the prevProductsQuantity constant is indeed populated
I created the function cartClear to receive the cart id and delete the document from the model but it is not working.
I am able to create many "same" orders so I think i need to manage with a conditiona but not sure where.
The response I am looking for:
{
"success": true,
"data": {
"_id": "607ed5c425ae063f7469a807",
"userId": "6071aed0ed7ec9344cf9616c",
"productsQuantity": [
{
"_id": "607f2507994d5e4bf4d91879",
"productId": {
"productRef": "463b8bb7-6cf6-4a97-a665-ab5730b69ba2",
"productName": "table",
"brand": "boehm llc",
"price": 713,
"__v": 0,
"createdAt": "2021-04-09T18:31:43.430Z",
"updatedAt": "2021-04-09T18:31:43.430Z"
},
"quantity": 2,
"updatedAt": "2021-04-21T15:12:51.894Z",
"createdAt": "2021-04-21T15:12:51.894Z"
}
],
"__v": 0
}
++ THE DELETION OF THE CART ++
The current response I am getting:
{
"success": true,
"data": {
"state": "pending-payment",
"_id": "6080507a0c758608d0dc585c",
"userId": "6071aed0ed7ec9344cf9616c",
"totalPrice": 1426,
"productsQuantity": [
{
"_id": "607f2507994d5e4bf4d91879",
"productId": "60709d8f24a9615d9cff2b69",
"quantity": 2,
"updatedAt": "2021-04-21T15:12:51.894Z",
"createdAt": "2021-04-21T15:12:51.894Z"
}
],
"createdAt": "2021-04-21T16:19:06.056Z",
"updatedAt": "2021-04-21T16:19:06.056Z",
"__v": 0
}
** AND THE CART IS NOT DELETING **
this is the code I typed for these purposes.
Any guidance is super appreciated
router.post('/', [isAuthenticated], async (req, res, next) => {
try {
const cart = await CartModel.findOne({ userId: req.user }).populate({
path: 'productsQuantity.productId',
select: {
price: 1,
brand: 1,
productName: 1,
productRef: 1,
pictures: 1
}
});
const prevProductsQuantity = cart
.get('productsQuantity')
.map((el) => el.toObject());
const totalPriceByProduct = prevProductsQuantity.map(
(product) => product.productId.price * product.quantity
);
const totalPrice = totalPriceByProduct.reduce(function (a, b) {
return a + b;
});
const result = await OrderModel.create({
userId: req.user,
totalPrice: totalPrice,
state: 'pending-payment',
productsQuantity: prevProductsQuantity
});
const cartClear = (id) =>
CartModel.deleteOne({
_id: id._id
});
res.status(200).json({
success: true,
data: result
});
cartClear(`${cart._id.toString()}`);
} catch (error) {
next(error);
}
});
You are not resolving the deleteOne promise, missing async & await.
const cartClear = async (id) =>
CartModel.deleteOne({
_id: id
});
await cartClear(`${cart._id.toString()}`)
res.status(200).json({
success: true,
data: result
});

Google People Api: Country not returned in Adresses field

I am trying to get the authenticated user's country by specifying "addresses" field in the People Api request as specified here.
This is the code:
router.post("/test_scope", (req, res) => {
const { idToken, accessToken } = req.body;
authenticationServices
.validateGoogleAccessToken(idToken, accessToken)
.then((response) => {
res.json(response);
});
});
const validateGoogleAccessToken = async (idToken, accessToken) => {
try {
const CLIENT_ID =
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com";
const client = new OAuth2Client(CLIENT_ID);
const ticket = await client.verifyIdToken({
idToken,
audience: CLIENT_ID,
});
const payload = ticket.getPayload();
const { OAuth2 } = google.auth;
const oauth2Client = new OAuth2();
oauth2Client.setCredentials({ access_token: accessToken });
const peopleAPI = google.people({
version: "v1",
auth: oauth2Client,
});
const { data } = await peopleAPI.people.get({
resourceName: "people/me",
personFields: "birthdays,genders,addresses",
});
const { birthdays, genders, addresses, ageRanges } = data;
return data;
} catch (error) {
console.log("error: ", error);
throw new Error("Google token validation failed");
}
};
I have set up the addresses information in the account I am using to sign-in like this:
The is the whole response I get after sending the request:
{
"resourceName": "people/XXXXXXXXXXXXXXXXx",
"etag": "%XXXXXXXXXXXXXXXXXXXXXXXXXX",
"genders": [
{
"metadata": {
"primary": true,
"source": {
"type": "PROFILE",
"id": "XXXXXXXXXXXXXXXXXXXX"
}
},
"value": "female",
"formattedValue": "Female"
}
],
"birthdays": [
{
"metadata": {
"primary": true,
"source": {
"type": "PROFILE",
"id": "1XXXXXXXXXXXXXXXXXXXXXXXX"
}
},
"date": {
"month": 8,
"day": 9
}
},
{
"metadata": {
"source": {
"type": "ACCOUNT",
"id": "XXXXXXXXXXXXXXXXXXXXXXx"
}
},
"date": {
"year": 1996,
"month": 8,
"day": 9
}
}
],
"addresses": [
{
"metadata": {
"primary": true,
"source": {
"type": "PROFILE",
"id": "111110367350060808978"
}
},
"formattedValue": "XXXXXXXXXXXXX, XXXXX, XXXXX",
"type": "home",
"formattedType": "Home"
}
],
}
As you see the country field is missing in "addresses.
NOTE: I am retrieving the idToken and the accessToken from the FrontEnd, after the user clicks on the SignIn button:
import GoogleLogin from "react-google-login";
export class LoginPage extends Component {
onGoogleSignInSucceeded(response) {
const { accessToken, tokenId } = response;
}
render() {
const { errors } = this.state;
return (
<>
<GoogleLogin
clientId="XXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"
scope="https://www.googleapis.com/auth/user.birthday.read https://www.googleapis.com/auth/user.gender.read https://www.googleapis.com/auth/user.addresses.read"
// IMPORTANT
// https://developers.google.com/people/api/rest/v1/people/get#authorization-scopes
buttonText="Sign-in with your Google account"
onSuccess={this.onGoogleSignInSucceeded}
onFailure={this.onGoogleSignInFailed}
cookiePolicy={"single_host_origin"}
/>
</>
);
}
}
All fields on the address field are optional (see docs). This also includes the country. There is also no guarantee that the data is actually correct (the user can add invalid data to their google profile), so be careful about that and check for the verified metadata (see docs).
That being said you could try using a geocoding API to get the country from the formatted address. This could be made by using a reverse geocoding query (Google Geocoding API documentation).
Also notice that there are other fields that may contain an address. For example locations (see docs) can contain information about where they live.

How to do nested loops with object jSON data on nodejs

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)

Categories

Resources