I am trying in mongoDB and node to add subscriptions to my competitionEvent object.
My issue is that I can write only one subscription and not add them one after another.
Here is my http file:
const express = require('express')
import * as bodyParser from 'body-parser'
// import { eventApplication } from './compositionRoot'
import { CompetitionModel } from './mongo'
export const app = express()
app.use(bodyParser.json())
// WORKS - find all events
app.get('/events', async (_req: any, res: any) => {
const comp = await CompetitionModel.find()
res.send(comp)
})
// WOKRS - find just one event
app.get('/events/:event_id', async (req: any, res: any) => {
const searchedComp = await CompetitionModel.find(req.params)
res.send(searchedComp)
})
// WORKS - posts a new comp event
app.post('/new-comp', async (req: any, res: any) => {
const data = await new CompetitionModel(req.body).save()
res.json(data)
})
// WORKS - posts a new subscription into a comp
app.put('/update/:event_id', async (req: any, res: any) => {
const subs = await CompetitionModel.findOneAndUpdate(
{ event_id: req.params.event_id },
{ subscriptions: req.body },
)
res.send(subs)
})
// TO TEST - deletes a competition event
app.delete('/delete/:event_id', async (req: any, res: any) => {
const toDel = await CompetitionModel.deleteOne({
event_id: req.params.event_id,
})
res.json(toDel)
})
and here is my mongo file:
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/CompetitionEvent')
export const CompetitionSchema = new mongoose.Schema({
event_id: String,
compName: String,
place: String,
time: String,
subscriptions: [],
date: Date,
cost: {
currency: String,
amount: Number,
},
})
export const CompetitionModel = mongoose.model(
'CompetitionModel',
CompetitionSchema,
)
export const connection = () =>
new Promise((resolve, reject) => {
mongoose.connection.once('open', () => {
resolve()
})
mongoose.connection.once('error', () => {
reject('oooooh shit')
})
})
Every time I tried to change it it would either not modify the competitionEvent, not put anything or simply replace the old subscription with a new one, which makes little sense I am sure you'll agree
You need to use the $push-operator to add a new subscription to your competition. Assuming req.body holds the new subscription, you can do:
app.put('/update/:event_id', async (req: any, res: any) => {
const subs = await CompetitionModel.findOneAndUpdate(
{ event_id: req.params.event_id },
{ $push: { subscriptions: req.body }},
)
res.send(subs)
});
First of all fix your schema for subscription mongoose.Schema like below, for better type casting:
Optional
const CompetitionSchema = new mongoose.Schema({
event_id: String,
compName: String,
place: String,
time: String,
subscriptions: [{
//what ever field you wanna add
_id: false //if you don't wanna use it as a sub-document
}],
date: Date,
cost: {
currency: String,
amount: Number,
},
})
Then in your competetionEvent controller either use mongo $push operator for adding event subscription at the end of the subscription or use mongo $addToSet operator for adding the subscription in the subscription field without any duplication.
Remember, $push doesn't check if the subscription is unique or not, it just pushes elements like javascript Array.push(). On the other hand, $addToSet checks if the subscription exists or not. If yes then it doesn't add that subscription. If no, then it pushes it to the field Array.
I suggest using $addToSet as it is more secure & will not create any duplicates of the same subscription.
CODE
app.put('/update/:event_id', async (req: any, res: any) => {
const subs = await CompetitionModel.findOneAndUpdate(
{ event_id: req.params.event_id },
{ $addToSet: {subscriptions: req.body}},
)
res.send(subs)
})
Related
I'm trying to create a user after he verified the code that I send him
so first I generate the code in sendCode resolver and save it in Redis using setex
the problem is that code is set in Redis but when I try to use it in createUser resolver using get it returns null.
const sendCode: MutationResolvers["sendCode"] = async ({
input: { phoneNumber, email },
}: {
input: SendCodeInput;
}) => {
const code = generate4digitNum();
await redis.setex(phoneNumber ?? email, THREE_MINS, code);
return {};
};
const createUser: MutationResolvers["createUser"] = async ({
input: { ...userData },
}: {
input: CreateUserInput;
}) => {
const code = await redis.get(userData.phoneNumber ?? userData.email);
if (code !== userData.code) {
throw new Error(errors[0].id);
}
user = await userModel.create({ ...userData});
return {type: user.type, _id: user._id };
};
the redis.ts file that I create:
const client = redis.createClient({
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASSWORD,
port: Number(process.env.REDIS_PORT),
});
client
.on("connect", function () {
console.log(`connected ${client.connected}`);
})
.on("error", function (error) {
console.log(error);
});
export const get: (key: string) => Promise<string> = promisify(client.get).bind(
client
);
export const setex: (
key: string,
seconds: number,
value: string
) => Promise<string> = promisify(client.setex).bind(client);
I will appreciate any kind of help.
Thanks in advance.
Please bear with me, I am a beginner in node and async stuff is still no super clear for me.
I have the below piece of code and I a now working on the last part - the /new-comp route.
It is supposed to post in the database I connected above:
import { Schema } from 'mongoose'
export const mongoose = require('mongoose')
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const urlEncodedParser = bodyParser.urlencoded({ extended: false })
mongoose.connect('mongodb://localhost:27017/CompetitionEvent')
export const db = mongoose.connection
db.on('error', console.error.bind(console, 'An error has occured: '))
db.once('open', function () {
console.log('Connected to Mongodb')
})
const CompetitionSchema = new Schema({
id: String,
place: String,
time: String,
subscriptions: [],
date: Date,
cost: {
currency: String,
amount: Number,
},
})
const CompetitionModel = mongoose.model('CompetitionModel', CompetitionSchema)
app.use(bodyParser.json())
app.get('/events', (_req: any, res: any) => {
res.send(eventApplication.getAll())
})
app.post('/event', async (req: any, res: any) => {
await eventApplication.createAnEvent(req.body)
res.json({
success: true,
})
})
app.post('/new-comp', urlEncodedParser, async (res: any, req: any) => {
await eventApplication.createAnEvent(req.body)
const newComp = CompetitionModel(req.body)
newComp.save(function (error: any, data: any) {
if (error) throw error
res.json(data)
})
})
app.listen(8000)
I also have this file that has all my classes:
export interface Subscription {
id: string
event_id: string
name: string
surname: string
}
export interface EventDTO {
id: string
place: string
time: string
subscriptions: Subscription[]
date: Date
cost: EventCost
}
export interface EventCost {
amount: number
currency: string
}
export class CompetitionEvent {
public subscriptions: Subscription[]
public place: string
public time: string
public date: Date
public cost: EventCost
public id: string
static save: any
constructor(data: EventDTO) {
this.subscriptions = data.subscriptions
this.place = data.place
this.time = data.time
this.date = data.date
this.cost = data.cost
this.id = data.id
}
public isCompleted = () => this.place === 'Poznan' && this.date === new Date()
public getSubs = () => this.subscriptions
public subscribe = (sub: Subscription) => {
this.subscriptions = [...this.subscriptions, sub]
return this
}
public cancelSubscription(subscription: Subscription) {
const subExists = this.subscriptions.find(
(it) => it.id === subscription.id && it.name === subscription.name,
)
if (!subExists) {
throw new Error('Subscription does not exist.')
}
this.subscriptions = this.subscriptions.filter(
(it) => it.id !== subscription.id,
)
}
}
Now my issue is that when I post some data to my app using curl, I have anerror message from the server as follows:
(node:3264) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'subscriptions' of undefined
I am not sure how to understand this log. It seems that I have an unhandled promise somewhere (I get lines in the log but sometimes it points to empty lines in my program".
Do you have any idea how I should understand / manage this issue?
Thanks in advance
I'm trying to test my resolvers but i'd like to test each field of the response, here's the code to call the response:
interface Options {
source: string;
variableValues?: Maybe<{ [key: string]: unknown | null }>;
}
let schema: GraphQLSchema;
const gCall = async ({
source,
variableValues,
}: Options): Promise<ExecutionResult> => {
if (!schema) {
schema = await createSchema();
}
return graphql({
schema,
source,
variableValues,
});
};
export default gCall;
And that's the code to test the resolver:
let connection: Connection;
const challengeMutation = `
mutation CreateChallenge($data: CreateChallengeInput!) {
createChallenge(data: $data) {
id
name
category
startDate
endDate
goal
description
}
}
`;
describe('Create Challenge', () => {
beforeAll(async () => {
connection = await databaseTestConnection();
await connection.createQueryBuilder().delete().from(Challenge).execute();
});
afterAll(async () => {
await connection.createQueryBuilder().delete().from(Challenge).execute();
await connection.close();
});
it('should create challenge', async () => {
const challenge = {
name: 'some awesome name',
category: 'distância',
startDate: new Date(2020, 7, 4).toISOString(),
endDate: new Date(2020, 7, 5).toISOString(),
goal: 5000,
description: 'some excelent challenge description',
};
const response = await gCall({
source: challengeMutation,
variableValues: {
data: challenge,
},
});
expect(response).toMatchObject({
data: {
createChallenge: {
name: challenge.name,
category: challenge.category,
startDate: challenge.startDate,
endDate: challenge.endDate,
goal: challenge.goal,
description: challenge.description,
},
},
});
});
});
What I'd like to do is test the fields separately, like this:
expect(response.data.createChallenge.name).toEqual(challenge.name);
But I'm getting the following error when I try to execute the above code:
Object is possibly 'null' or 'undefined'.
What can I do to solve this error and to make this test better?
Object is possibly 'null' or 'undefined'.
TypeScript warns you that the response data might not exist as the graphql "server" might return error instead. So you should use ! operator to assert it's not null.
You should also do that after checking it's not undefined with expect().
I'm working on an tiny app that allows user to participate in polls, but I'm having problems checking if the current user has already voted in the poll. Everything else works fine, save for the IIFE that checks for said condition, as seen in the code snippet included. Indeed, I'm getting false as opposed to true with the data I have i.e. I already seeded the DB with sample data, including a random poll that contains the array of IDs for users who've already voted. I tried testing one ID against said array, which returns false as opposed to the expected true. What gives?
Below are the relevant snippets.
Model
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
const ChoiceSchema = new Schema({
name: { type: String },
votes: { type: Number }
});
const PollSchema = new Schema({
title: { type: String },
category: { type: String },
choices: [ChoiceSchema],
addedBy: { type: Schema.Types.ObjectId, ref: 'User' },
votedBy: [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
const Poll = mongoose.model('Poll', PollSchema);
export default Poll;
Controllers
import Poll from '../models/poll';
export default {
fetchAllPolls: async (req, res) => {
/*...*/
},
fetchSpecificPoll: async (req, res) => {
/*...*/
},
voteInPoll: async (req, res) => {
const { category, pollId } = req.params;
const { name, choiceId, voterId } = req.body;
try {
const poll = await Poll.findById(pollId);
const choice = await poll.choices.id(choiceId);
const votedChoice = {
name,
votes: choice.votes + 1,
};
// Check if user has already voted in poll
const hasVoted = ((votersIds, id) => votersIds.includes(id))(
poll.votedBy,
voterId
);
if (!voterId) {
res
.status(400)
.json({ message: 'Sorry, you must be logged in to vote' });
} else if (voterId && hasVoted) {
res.status(400).json({ message: 'Sorry, you can only vote once' });
} else {
await choice.set(votedChoice);
await poll.votedBy.push(voterId);
poll.save();
res.status(200).json({
message: 'Thank you for voting. Find other polls at: ',
poll,
});
}
} catch (error) {
res.status(404).json({ error: error.message });
}
},
createNewPoll: async (req, res) => {
/*...*/
},
};
I think you are trying to compare ObjectId with String representing the mongo id.
This should work:
const hasVoted = ((votersIds, id) => votersIds.findIndex((item) => item.toString() === id) !== -1)(
poll.votedBy,
voterId
);
or
const hasVoted = ((votersIds, id) => votersIds.findIndex((item) => item.equals(new ObjectId(id))) !== -1)(
poll.votedBy,
voterId
);
EDIT:
As #JasonCust suggested, a simpler form should be:
const hasVoted = poll.votedBy.some(voter => voter.equals(voterId));
It is more than likely that poll.votedBy is not an array of ID strings. If you are using it as a reference field then it is an array of BSON objects which would fail using includes because it uses the sameValueZero algorithm to compare values. If that is true then you could either convert all of the IDs to strings first or you could use find and the equals methods to find a match.
Update: showing actual code example
Also, some would provide an easier method for returning a boolean value.
const hasVoted = poll.votedBy.some((voter) => voter.equals(voterId));
I'm using express, passport, and mongoose. I don't know why but the code below pushes same newTaxReturn._id twice into user.taxReturnIds field. If I remove user.save().catch(() => {}) line, it pushes the newTaxReturn._id correctly i.e. just once. The user argument is from passport.
Problem:
const createTaxReturn = ({ user }) => {
const newTaxReturn = new TaxReturn({ userId: user._id })
user.taxReturnIds.push(newTaxReturn._id)
user.save().catch(() => {})
return newTaxReturn.save().catch(() => {})
}
Schema:
const User = new mongoose.Schema({
taxReturnIds: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'TaxReturn',
}],
})
const TaxReturn = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
})
On your return you are also calling .save() thus the duplication and the single input when you remove
user.save().catch(() => {})
place your return in a .then or .catch to retrieve the response from mongo
user.save().catch(error => { if (error) return error })