Mongoose FindOne Query Still Trying to Cast to ObjectID - javascript

The following code is throwing the following error:
await this.GuildModel.findOne({ _id: guildId }).exec((guild) => {
console.log(guild);
});
CastError: Cast to ObjectId failed for value "894673322397302784" (type string) at path "_id" for model "GUILD"
{
messageFormat: undefined,
stringValue: '"894673322397302784"',
kind: 'ObjectId',
value: '894673322397302784',
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
My _id value in my schema and in my database is of type string, and hence I do not want it to be casted to an ObjectID, my schema:
const GuildSchema = {
_id: { type: String },
prefix: { type: String, default: "!" },
}
After doing some research, I realised that I couldn't use findById as it forces it into using an ObjectID, hence why I used findOne. How would I query by my _id which is of type string.

_id is reserved for ObjectID my MongoDB, use id(not _id) for your own generated ID, and in your query use id also, you should be fine.

Related

AWS Datastore GraphQL Unable to Query many to many relations

I am currently following this workflow on the AWS Amplify docs:
https://docs.amplify.aws/lib/datastore/relational/q/platform/js/#many-to-many-relationships
Yet I cannot query my relational join tables. I am confused as to what I have done incorrectly in terms of syntax. This is my code and these are the errors I receive.
// schema.graph.ql
type Icd10 #model #auth(rules: [{allow: public}]) {
id: ID!
Code: String
Description: String
MedicalTests: [MedicalTest] #manyToMany(relationName: "Icd10MedicalTest")
Disease: [Disease] #manyToMany(relationName: "Icd10Disease")
}
type MedicalTest #model #auth(rules: [{allow: public}]) {
AdditionalIcd10: [String]
AdditionalTesting: String
ClinicalUtility: String
Cpt: [String]
DescriptiveMethodology: String
Icd10: [String]
Icd9: [String]
Interpretation: String
Loinc: [String]
Methodology: String
Name: String
Overview: String
PanelTests: [String]
ReferenceRanges: String
References: String
SpecimenCollection: String
SpecimenType: String
Synonyms: [String]
TurnaroundTime: String
createdAt: AWSDateTime!
id: ID!
updatedAt: AWSDateTime!
uuid: String
ratings: [Rating] #manyToMany(relationName: "RatingMedicalTest")
icd10s: [Icd10] #manyToMany(relationName: "Icd10MedicalTest")
}
// TransferData.js
const value = await DataStore.query(MedicalTest,
m => m.Name.eq("11-Dehydro-Thromboxane B2, Urine")
)
console.log(value)
const result = await DataStore.query(Icd10,
m => m.MedicalTests.MedicalTest.id.eq(value.id)
)
console.log("updateIcd10MedicalTest Successful!")
console.log(result)
Error Image
I have attempted to change the input values multiple times as I thought maybe I was wrong in reading how my schema functioned as I'm new to GraphQL.

Cast to ObjectId failed for value \"\" (type string) at path \"category\" because of \"BSONTypeError\

I have the Cast to error in JSON response when Adding the Product Details in the & when adding category showing Error.
const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;
{
category: {
type: ObjectId,
ref: "Category",
},
};
Except category Every detail is adding in the mongodb table
You should try mongoose.SchemaTypes.ObjectId
#Prop({ type: mongoose.SchemaTypes.ObjectId, ref: 'ExercisePlan' })
category: string;
And if you are trying to aggregate with _id so you should try
$match: {
category: new Types.ObjectId(inputId),
},

how to return a Mongoose nested array of objects

I am trying to return a nested Object that I declared in my mongoose model as follows:
const MessageSchema = new Schema({
messageLog: [
{
transcript: {
type: String
},
recipient: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
sender: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
}
}]
});
however I am not able to get the value for the inner object back when I try to query for it via a graphql resolver (transcript, sender, recipient are null on gql playground, but updated in db) I have set it up as follows:
query = args.messageId ? { _id: args.messageId } : { _id: new ObjectId() };
const message = await Message.findOneAndUpdate(query, {$addToSet: {messageLog: {transcript: args.messageBody, sender: args.senderId, recipient: args.recipientId}}}, {$setOnInsert: args, upsert: true, new: true, runValidators: true})
return message.messageLog;
I am able to create the new object and the nested messageLog in the db but I can only return the id for some reason as opposed to the the messageLog array of objects. Usually the issue lies in how I am resolving (resolvers) but I am going to put my typeDef here as well in case the issue lies there.
type Message {
_id: ID
transcript: [String]
recipient: [User]
sender: [User]
}
So the solution in case anyone has a similar schema setup and issue is to reference the the typeDefs with the nested levels as well. So since transcript, recipient and sender were nested a level down, the typeDef would have to be defined for the nested object and then referenced on the message type as follows:
type messageLog {
_id: ID
transcript: String
recipient: User
sender: User
}
type Message {
_id: ID
messageLog: [messageLog]
}
and to use populate for the User since it was a schema referenced by the objectId

How to make multiple connects in one GQL mutation?

So I have the following in my Prisma datamodel. There's a relationship between the games and the characters, where the characters can appear in multiple games. Also the characters should be able to be listed on the games when the games alone are queried:
type Game {
id: ID! #unique
name: String! #unique
name_ja: String #unique
name_ko: String #unique
name_zh_CN: String #unique
name_zh_TW: String #unique
name_zh_HK: String #unique
characters: [Character]
filters: [GameFilter]
}
type Character {
id: ID! #unique
name: String! #unique
name_ja: String #unique
name_ko: String #unique
name_zh_CN: String #unique
name_zh_TW: String #unique
name_zh_HK: String #unique
games: [Game]
}
I then have this as a mutation for updating characters in my schema. I pass along an array of Game IDs because there's the possibility of multiple games being added within the same mutation:
updateCharacter(
name: String
name_ja: String
name_ko: String
name_zh_CN: String
name_zh_HK: String
name_zh_TW: String
id: ID!
games: [ID]
): Character!
And the following mutation written:
async updateCharacter(parent, args, ctx, info) {
if (!ctx.request.userId) {
throw new Error('You must be logged in');
}
const updates = {...args};
delete updates.id;
delete updates.games;
console.log(args.games);
const res = await ctx.db.mutation.updateCharacter({
data: updates,
games: {
connect: {
id: args.games
}
},
where: {
id: args.id
}
}, info);
return res;
}
And the following mutation written React-side. It's meant to pass along an array of IDs to the mutation (which will be expected because the schema assumes that an array of IDs will be passed along:
const UPDATE_CHARACTER_MUTATION = gql`
mutation UPDATE_CHARACTER_MUTATION(
$name: String!
$name_ja: String!
$name_ko: String!
$name_zh_CN: String!
$name_zh_TW: String!
$name_zh_HK: String!
$id: ID!
$games: [ID]!
) {
updateCharacter(
name: $name
name_ja: $name_ja
name_ko: $name_ko
name_zh_CN: $name_zh_CN
name_zh_TW: $name_zh_TW
name_zh_HK: $name_zh_HK
games: $games
id: $id
) {
id
name
name_ja
name_ko
name_zh_CN
name_zh_TW
name_zh_HK
games {
id
name
}
}
}
`;
I am also passing along the IDs of the games in an array onSubmit within the form to the mutation.
The result is that the mutation passes, but will only update the name fields, and the connection with the Games remains unchanged.
Not sure what I can do to make this mutate properly. I'm not sure whether passing the array directly into the mutation in the connect is what's causing the issue, but I tried mapping through and passing along each game id individually, but had the same result of the connection not updating.
The problem seems to be two-fold.
Regarding not seeing any changes at all:
In the mutation written, games is added to the async request json outside of data, meaning the data will never be found by the request.
Rather than:
data: ...,
games: ...
games must be part of data like such:
data: {
games: ...
}
Regarding wanting to do multiple connects:
GraphQL supports this style of nested updates using the following connect syntax:
connect: [
{ id : /* id from args */ },
{ id : /* another id from args */ },
...
]

Mongoose saving empty array

I have a array and i trying to insert it with mongoose but in return i got a empty array what i'm doing wrong,
my schema:
let postSchema = mongoose.Schema({
date : { type: Date, default: Date.now },
name : String,
desc : String,
type : []
},{collection: 'Post'});
my insert:
console.log(req.body.type); //here i have something like ["Teste1","Teste2"]
let post = new Post({
name: req.body.descricao,
desc: req.body.desc
type: req.body.type
});
post.save((err, model) => {
if (err) console.log(err);
if (model) console.log(model); //ALL INSERTED but array is just type:[]
})
The best way is to specify the type of the array elements.
For example,
type: [String]
Specifying an empty array is equivalent to Mixed type.
Also check the type of req.body.type
console.log(typeof req.body.type)

Categories