How to retrieve the mongodb data before updating them? - node.js

I need to write a nodejs api where I need to create and update a logo.
The create works fine, but while updating the logo with a given id, the previous logo should get deleted and update with the new logo. We can use fs.unlink to delete the logo; but while using fs.unlink, the code deleted the updated logo; how do I fix this?
If I somehow knew the previous logo's name, then I could delete it.
const data = await PRISMA.ref_types.update({
where: { id: id },
data: {
business_name: business_name,
business_logo: File_Name,
business_type: business_type,
address: address,
timezone: timezone,
currency: currency,
ref: headerData?.ref
},
});
// Update the existing image record in the database
const existingImage = await PRISMA.ref_types.findFirst({
where: { id }, select: {
business_logo: true
}
})
// find the image
const a = Object.entries(existingImage)
const b = a[0][1]
const fileName = b.split('/').pop()
console.log("dsadsad", fileName)
// Delete the old image file
fs.unlinkSync('uploads/' + headerData?.ref + '/settings/' + fileName)
let found = await PRISMA.languages.findFirst({
where: { ref: headerData.ref }
})

Related

Add a number to everyticket & link new made channel in message

So I want to make it so that every time a new ticket is made it will add a number example: ticket-1 | ticket-2 | ticket-3, ect. And then I want the bot to send the channel in a chat
module.exports = {
data: {
name: `GT1`
},
async execute(interaction, client, message) {
const guild = client.guilds.cache.get("1057116059750117426");
const ticketId = Math.floor(Math.random() * 9000);
await guild.channels.create({
name: `TICKET-${ticketId}`,
parent: '1057370813357109308',
})
interaction.reply({ephemeral: true, content: `Your ticket has been submited \n You can view it here -> ${guild.channels.id}` });
}
}
What you need is a way to persist data after every command. This would require some sort of data storage. I've listed a few options below:
Use a database, (On the discord.js guide they recommend using an ORM)
Store the files in a JSON object on your file system.
Here is an example for 2:
module.exports = {
data: {
name: 'GT1',
},
async execute(interaction, client, message) {
const guild = client.guilds.cache.get('1057116059750117426');
const storageBuffer = fs.readFileSync('./storage.json'); // you will input your file path here.
const storageData = JSON.parse(storageBuffer.toString());
storageData.ticket_id++; // adds one to the ticket number?
const ticketId = storageData.ticket_id;
await guild.channels.create({
name: `TICKET-${ticketId}`,
parent: '1057370813357109308',
});
interaction.reply({
ephemeral: true,
content: `Your ticket has been submited \n You can view it here -> ${guild.channels.id}`,
});
fs.writeFileSync('./storage.json', JSON.stringify(storageData)); // updates the data to your storage file
},
};
You will need to create the json file before using it.
storage.json
{
ticket_id: 0
}
As for sending to a channel you can take a look at this: https://discord.js.org/#/docs/main/stable/class/Interaction?scrollTo=channel

Mongoose document _id is null, so when I try to save I get MongooseError: document must have and id before saving

I'm making a discord bot to scrape prices from Amazon. Im using a mongoDB database to store links users give to the bot to track the price of the item the link leads to.
My issue is when I run my code and use the add command, my console reads...
Starting...
Online! Logged in as Amazon Price Tracker#6927
Connected to Database
null
MongooseError: document must have an _id before saving
at C:\Users\logic\Documents\Disc Bot\node_modules\mongoose\lib\model.js:291:18
at processTicksAndRejections (node:internal/process/task_queues:78:11)
Disconnected from Database
I've read the doc's and my understanding is mongoose generates a unique id automatically. I am aware that you can override this my defining an id in your schema, but I haven't done this so I don't know why console.log(a) prints null, and the .save() errors out.
My add.js file
//add function using mongoose for mongodb
const { SlashCommandBuilder } = require("#discordjs/builders");
const mongoose = require("mongoose");
const { MongoDBurl } = require("../config.json");
const Link = require("../Schemas/Link.js");
module.exports = {
//Build the slash command
data: new SlashCommandBuilder()
.setName("add")
.setDescription("add a url to watch list")
.addStringOption(option =>
option.setName("url")
.setDescription("url to add to watch list")
.setRequired(true),
),
//Function that runs when the command is used
async execute (interaction) {
const URL = interaction.options.getString("url");
const user = interaction.user.username;
await interaction.reply(`On it! Adding ${URL} to your watch list`)
//Connect to the database, throws an error if it can't connect
await mongoose.connect(MongoDBurl)
.then( () => console.log("Connected to Database"))
.catch(err => console.log(err));
//Check if the link is already in the database
var exists = await Link.exists({ link: URL}).exec()
.catch(err => console.log(err))
if (exists) {
console.log("This Document Already Exists")
interaction.editReply(`Oops! That link is already in my database.`)
} else {
//If the link dosen't exist, create a document and save it to the database
var newLink = new Link({ user: user }, { link: URL }, { price: "N/A" })
// Debuging variable
var a = newLink.id;
console.log(a)
await newLink.save()
.then( () => {
console.log("Document Saved")
interaction.editReply(`All done! I have saved ${URL} to your watch list.`)
})
.catch(err => {
console.log(err)
interaction.editReply("Oops! Something went wrong, I wasen't able to save this link.")
})
}
//Close the connection when we finish
await mongoose.connection.close()
.then( () => console.log("Disconnected from Database"))
}
};
My Link.js file
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const LinkSchema = new Schema({
user: {
type: String,
requiered: true
},
link: {
type: String,
requiered: true
},
price: {
type: String,
requiered: true
},
})
module.exports = mongoose.model("Link", LinkSchema);
When creating a new modal, the options must be within the same pair of curly braces, however when updating, its separate since you are changing multiple elements.
That's why the error was occurring. You have already shared a working piece of code so I'm guessing you no longer need one.
So I found my issue. I changed this line
var newLink = new Link({ user: user }, { link: URL }, { price: "N/A" })
To
const newLink = new Link({ user: user, link: URL, price: "N/A" });
I don't know why this fixed it, I don't think its because I changed var -> const, and looking at the documentation I thought the first line was the correct way to do this
The line I originally used from the documentation
Tank.updateOne({ size: 'large' }, { name: 'T-90' }, function(err, res) {
// Updated at most one doc, `res.nModified` contains the number
// of docs that MongoDB updated
});
Is this an error in the documentation? or a possible bug? either way the issue is now resolved.

Firebase Cloud Function batch write update document overwrote the entire document?

The following Cloud Function has a batch write operation that, in part, updates a single field in a document. This overwrote the entire document and now the document has a single field joinedCount: -1. Is this not the way to update individual fields in documents without overwriting them?
exports.deleteUserTEST = functions.https.onCall(async (data, _context) => {
const uId = data.userId;
const db = admin.firestore();
try {
const batch = db.batch();
const settingsDoc = await db.collection("userSettings").doc(uId).get();
const joinedIds = settingsDoc.get("private.joinedIds");
Object.keys(joinedIds).forEach(function(jId, _index) {
batch.update(
db.collection("profiles").doc(jId),
{
private: {
joinedCount: admin.firestore.FieldValue.increment(-1), // <-- the culprit
},
},
);
});
await batch.commit();
} catch (error) {
throw new functions.https.HttpsError("unknown", "Failed the delete the user's content.", error);
}
return Promise.resolve(uId);
});
Moving the solution found in the comments by #Dharmaraj into a community answer, this problem was caused by the structure of the document.
Since all the data in the document was inside the private map field, passing a new map through the update method would make it appear that the entire document was being overwritten instead of updated.
In this case, you would need to access the fields through dot notation. This allows those inner fields within the map to be updated, without replacing the entire private map:
Object.keys(joinedIds).forEach(function(jId, _index) {
batch.update(db.collection("profiles").doc(jId), {
"private.joinedCount": admin.firestore.FieldValue.increment(-1)
});
});
Another example from the documentation:
import { doc, setDoc, updateDoc } from "firebase/firestore";
// Create an initial document to update.
const frankDocRef = doc(db, "users", "frank");
await setDoc(frankDocRef, {
name: "Frank",
favorites: { food: "Pizza", color: "Blue", subject: "recess" },
age: 12
});
// To update age and favorite color:
await updateDoc(frankDocRef, {
"age": 13,
"favorites.color": "Red"
});

Update document in MongoDB via NodeJS

So my knowledge of NodeJS and MongoDD are non-existent (just need to do a small code update for a friend) and I'm stuck.
Need to update a single document inside a collection via a unique id but can't seem to do it.
Here's the Model (I've trimmed it down and cut out all unnecessary data). I'm trying to update the field notes inside a transaction.
In short each entry in the given (an Agent) table will have a collection of multiple Transactions & Documents. I need to update a specific Transaction with the unique _id that is auto generated.
import { Schema, model } from 'mongoose';
interface Transaction {
first_name: string;
last_name: string;
type: string;
notes: string;
}
interface Agent {
org_id: number;
transactions: Array<Transaction>;
documents: Array<string>;
}
const transactionSchema = new Schema<Transaction>({
first_name: { type: String },
last_name: { type: String },
type: { type: String },
notes: String,
});
const transactionsSchema = new Schema<Agent>({
org_id: { type: Number },
transactions: [transactionSchema],
documents: [documentTypesSchema],
});
const AgentTransaction = model<Agent>(
'agent_transaction_table',
transactionsSchema
);
export default AgentTransaction;
Here's what I tried but didn't work (obviously), again I've trimmed out all unnecessary data. Just to clarify, the endpoint itself works, but the DB update does not.
import AgentTransaction from '../models/transaction'; // the above model
transaction.put('/notes', async (req, res) => {
const { org_id, transaction_id, notes } = req.body;
try {
const notesResult = await AgentTransaction.updateOne({
'transactions._id': transaction_id,
}, {
$set: {
'notes': notes
},
});
res
.status(200)
.json({ message: 'Updated', success: true, notesResult });
} catch (error) {
res.status(400).send(error);
}
});
So I figured it out. Maybe it'll help someone else as well.
const notesResult = await AgentTransaction.updateOne({
'transactions._id': { $in: [trunc2] },
}, {
$set: {
'transactions.$.notes': notes
},
});
The main issue was that the payload object needed to target the collection folder + the wildcard + the field, not just only the field.

Why firestore document is not creating nor updating data?

Problem
Firestore document is not creating nor updating data
let user = db.collection('users').doc(userId);
let data = {
timestamp: FieldValue.serverTimestamp(),
bounties : {
[impressionId] : {
timestamp: timestamp,
amount: amount,
currency: currency
}
}
};
user.set(data, {merge: true});
Expectation
The below example data should be use to create or update cloud firestore document
{
"example-user-1": {
"bounties": {
"example-impression-1": {
"timestamp": "12315443",
"amount": 0.0,
"currency": "null"
}
}
}
}
Results
The document is not created
{"domain":{"domain":null,"_events":{},"_eventsCount":1,"members":[]}}
Option 1
After installing this library and adding this code, it works! but I dont know why.
const {Firestore} = require('#google-cloud/firestore');
// Create a new client
const firestore = new Firestore();
async function quickstart() {
// Obtain a document reference.
const document = firestore.doc('posts/intro-to-firestore');
// Enter new data into the document.
await document.set({
title: 'Welcome to Firestore',
body: 'Hello World',
});
console.log('Entered new data into the document');
// Update an existing document.
await document.update({
body: 'My first Firestore app',
});
console.log('Updated an existing document');
// Read the document.
let doc = await document.get();
console.log('Read the document');
// Delete the document.
await document.delete();
console.log('Deleted the document');
}
quickstart();
user.set(data, {merge: true}).then(data=>{console.log(data)});

Resources