I'm trying to query cassandra with id but I get this error
apollo.model.validator.invalidvalue: Invalid Value: "{ id: [Uuid] }" for Field: id (Type: uuid)
In my code I have :
const reqId = models.uuidFromString(req.params.id);
const queryObj = { id: reqId };
this.findById(queryObj, function(err, user)
My model:
id: {
type: "uuid",
rule: {required: true},
default: {$db_function: "uuid()"}
}
Related
I want to assign the same id value to roomID where the objects have the same type.
const Item = new Schema({
roomID: { type: String},
type: { type: String }
});
const Order = new Schema({
items: [{ type: Item }]
});
I want the output to be something like this:
{
id: "1234567890",
items: [
{
roomID:"5efd1db1803ec01b1c590e23"
type: "first",
// more data
},
{
roomID:"5efd1db1803ec01b1c590e23"
type: "first",
// more data
},
{
roomID:"5efd1db1803ec01b1c590e82"
type: "second",
// more data
},
],
};
I'am having troubles with graphql input types.
I'am using react with apollo
i have this working mutation:
addQuestion(
question: QuestionInput!
): Question!
with this type:
type QuestionInput {
name: String!
account_id: Int!
chatbot_id: Int!
question_variants: [String!]!
answer: String!
}
but the problem happens with this mutation:
updateBranch(
id: Int!
branch: BranchInput!
): Branch!
with this type:
type BranchInput {
lead_id: Int!
title: String!
visible_order: Int!
bot_photo: String!
responses: [ResponseInput!]!
bubbles: [BubbleInput!]!
}
and these nested types:
type ResponseInput {
branch_id: Int
owner: String!
message: String!
}
type BubbleInput {
branch_id: Int
name: String!
goto: String!
}
the mutation works on graphql playground:
mutation {
updateBranch(
id: 2
branch: {
lead_id: 1
title: "new title"
visible_order: 1
bot_photo: "photo"
responses: [
{ message: "message 1", owner: "owner1" }
{ message: "message 2", owner: "owner 2" }
]
bubbles: [{name: "bubble 1", goto: "link"}]
}
) {
id
title
}
}
When i execute the mutation in code like this :
const handleEditBranche = async (
lead_id: number,
title: string,
visible_order: number,
bot_photo: string,
responses: {}[],
bubbles: {}[],
id?: string
) => {
const newResponses = responses.map((response: any) => [
{ message: response.message, owner: response.owner },
]);
const newBubbles = bubbles.map((bubble: any) => [
{ name: bubble.name, goto: bubble.goto },
]);
console.log(id, title, visible_order, bot_photo, newResponses, newBubbles);
await updateBranche({
variables: {
id,
branch: {
title,
lead_id,
visible_order,
bot_photo,
responses: newResponses,
bubbles: newBubbles,
},
},
});
//setShowEdit({});
};
I get this error :
even though the data are correct:
I want to include those 2 type inside my mutation, but i don't know how.
The issue is with the way you are constructing the payload.
const newResponses = responses.map((response: any) =>
{ message: response.message, owner: response.owner },
);
const newBubbles = bubbles.map((bubble: any) =>
{ name: bubble.name, goto: bubble.goto },
);
console.log(id, title, visible_order, bot_photo, newResponses, newBubbles);
Removed the additional array for each element in newResponses and newBubbles
I have an array p_id which holds ObjectId of a Schema named "Panel". Now I want to find candidates from "Candidate" schema which have parentPanel same as id's present in p_id array.
This is the candidateSchema:
var candidateSchema = new Schema ({
name: {
type: String,
required: true,
},
votedBy: [{
type: Schema.Types.ObjectId,
ref: 'User',
default: null,
}],
parentPanel: {
type: Schema.Types.ObjectId,
ref: 'Panel'
}
});
I tried something like this;
Candidate.find({parentPanel: {$in: [p_id]})
.then(candidates => {
res.send(candidates)
})
.catch(err=>console.log(err));
This is the error I'm getting:
message: 'Cast to ObjectId failed for value "[
5d8734d2bf53280d76a3915a, ' +
'5d8734d2bf53280d76a3915e ]" at path "parentPanel" for model ' +
'"Candidate"', name: 'CastError', stringValue: '"[ 5d8734d2bf53280d76a3915a, 5d8734d2bf53280d76a3915e ]"', kind:
'ObjectId', value: [ 5d8734d2bf53280d76a3915a,
5d8734d2bf53280d76a3915e ], path: 'parentPanel', reason:
undefined, model: Model { Candidate }
Here is a relevant part of my Schema, where I'll make reservations to a "space":
var spaceSchema = new mongoose.Schema({
spaceName: String,
scheduledDates: [{
scheduledDates: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
username: String
}
}]
});
Author should be the current user that's logged in. Here is my route to update those fields:
router.put('/:space_id/schedule', function(req, res) {
Space.findByIdAndUpdate(req.params.space_id, {
'$push': { 'scheduledDates': req.body.space, 'author': req.user._id }
}, { "new": true, "upsert": true }, function(err, space) {
if (err) {
console.log(err);
} else {
console.log(req.body.space);
}
});
});
I can't access "author" correctly, because it's inside the array. What can I do to update this array, adding a new date and user to make the reservation?
Thank you
UPDATE
I tried to use "_id" instead of "id" in my property but got the same result. It seems like it's ignoring the "author" field, and only saving "scheduledDates"
So the schema was like this:
scheduledDates: [{
scheduledDates: String,
author: {
_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
username: String
}
}]
And then in my route, I changed what I was 'pushing':
'$push': { 'scheduledDates': req.body.space, 'author._id': req.user._id }
UPDATED 2
Changed the way I was getting the object to push:
'$push': {
'scheduledDates': {
'scheduledDates': req.body.space,
'author': { _id: req.user._id, username: req.user.username }
}
}
Now I'm getting the following error:
message: 'Cast to string failed for value "{ scheduledDates: \'04/11/2017\' }" at path "scheduledDates"',
name: 'CastError',
stringValue: '"{ scheduledDates: \'04/11/2017\' }"',
kind: 'string',
value: [Object],
path: 'scheduledDates',
reason: undefined } } }
I have this code:
router.post('/setsuggestions', function(req, res, next){
if(!req.body.username || !req.body.challengessuggestions){
return res.status(400).json({message: challengessuggestions});
}
var query = { username: req.body.username };
/*User.findOneAndUpdate(query, { challengessuggestions: req.body.challengessuggestions }, callback = function(response){
res.json(response);
});*/
/*
User.findOneAndUpdate(
query,
{$push: {"challengessuggestions": {$oid: req.body.challengessuggestions}}},
callback = function(response) {
res.json(response);
}
);*/
User.findOneAndUpdate(
query,
{$push: {challengessuggestions: req.body.challengessuggestions}},
{safe: true, upsert: true},
function(err, model) {
res.json(err);
}
);
});
When I postman like this:
I get the following error:
{ "name": "MongoError", "message": "exception: The field
'challengessuggestions' must be an array but is of type OID in
document {_id: ObjectId('56263b910d1a2f1f0077ffae')}", "errmsg":
"exception: The field 'challengessuggestions' must be an array but is
of type OID in document {_id: ObjectId('56263b910d1a2f1f0077ffae')}",
"code": 16837, "ok": 0 }
This is the schema definition of AppUser:
var UserSchema = new mongoose.Schema({
username: { type: String, lowercase: true, unique: true },
firstname: { type: String},
lastname: { type: String},
difficulty: { type: String},
isstudent: { type: Boolean },
haschildren: { type: Boolean},
gender: { type: String },
email: { type: String, unique: true},
birthdate: String,
isdoingchallenges: { type: Boolean },
challengescompleted: [{ type: ObjectId, ref: 'Challenge' }],
currentchallenge: { type: ObjectId, ref: 'Challenge' },
challengessuggestions: [{ type: ObjectId, ref: 'Challenge' }],
hash: String,
salt: String
});
This is the schema definiton of challenge:
var Challengeschema = new mongoose.Schema({
name: { type: String, initial: true, required: true, index: true },
image: { type: Array },
difficulty: { type: String },
studentfriendly: { type: Boolean },
childfriendly: { type: Boolean },
description: { type: String }
});
I'm sending this in the function that calls the api:
Object {_id: "5631423f8c5ba50300f2b4f6", difficulty: "medium", name:
"Probeer 1 van onze recepten.", __v: 0, childfriendly: true…}
This gives me following error:
D:\Stijn\Documenten\EVA-project-Groep-6\Api\node_modules\mongoose\lib\schema\obj
ectid.js:134
throw new CastError('ObjectId', value, this.path);
^ Error
at MongooseError.CastError (D:\Stijn\Documenten\EVA-project-Groep-6\Api\node
_modules\mongoose\lib\error\cast.js:18:16)
at ObjectId.cast (D:\Stijn\Documenten\EVA-project-Groep-6\Api\node_modules\m
ongoose\lib\schema\objectid.js:134:13)
at Array.MongooseArray.mixin._cast (D:\Stijn\Documenten\EVA-project-Groep-6\
Api\node_modules\mongoose\lib\types\array.js:124:32)
at Array.MongooseArray.mixin._mapCast (D:\Stijn\Documenten\EVA-project-Groep
-6\Api\node_modules\mongoose\lib\types\array.js:295:17)
at Object.map (native)
at Array.MongooseArray.mixin.push (D:\Stijn\Documenten\EVA-project-Groep-6\A
pi\node_modules\mongoose\lib\types\array.js:308:25)
at Query. (D:\Stijn\Documenten\EVA-project-Groep-6\Api\routes\ind ex.js:144:44)
at D:\Stijn\Documenten\EVA-project-Groep-6\Api\node_modules\mongoose\node_mo
dules\kareem\index.js:177:19
at D:\Stijn\Documenten\EVA-project-Groep-6\Api\node_modules\mongoose\node_mo
dules\kareem\index.js:109:16
at doNTCallback0 (node.js:408:9)
at process._tickCallback (node.js:337:13) 29 Oct 22:05:38 - [nodemon] app crashed - waiting for file changes before starti ng...
How do I solve this?
Query the User user using findOne() first and use the first found document that's passed to the callback to save the embedded documents with:
router.post('/setsuggestions', function(req, res, next){
if(!req.body.username || !req.body.challengessuggestions){
return res.status(400).json({message: challengessuggestions});
}
var query = { username: req.body.username };
User.findOne(query, function (err, user){
if (err) //throw ...
if (user) {
if (user.challengessuggestions && user.challengessuggestions.length) {
user.challengessuggestions.push(req.body.challengessuggestions);
}
else {
user.challengessuggestions = [req.body.challengessuggestions];
}
// save changes
user.save(function (err) {
if (!err) {
// done ...
}
});
}
});
);