I'm trying to return from our db a query where:
event_id = event.attributes.id
user_to = user.id
stripeCharge IS NOT NULL
I think I can do this use the has feature in bookshelf. I can't seem to get both the where & has going at the same time. Here is what my query that works with just where:
analyticsPayments.where({
event_id: event.attributes.id,
user_to: user.id
}).fetch().then(function(payment) {
console.log(payment);
}).catch(function(err) {
console.log(err);
res.status(500).json(err);});
I tried putting in after the where .has(stripeCharge) but it didn't like that.
Any idea how to achieve what I am trying to do, or how to have both a where and a has clause?
Thanks
Related
In my To-Do app, when a logged-in User completes a task, I would like to clear it from the MongoDB database.
Here is the code for my Schema.
const user = new mongoose.Schema({
username : String,
password : String,
task : [{
text : String,
day : String,
reminder : Boolean,
]}
})
For example, if Daryl completed text : "Gym" & day : "Feb 4th 5.30pm", I would like to only remove task[0] from Daryl's task Array.
Here is my attempt at doing so using Mongoose,
app.delete("/tasks", (req,res) => {
User.findOne( {_id : req.user.id}).then((target) => {
target.task.remove({text : req.body.text, day : req.body.day})
})
})
User.findOne({_id : req.user.id}) to only target the person that logged in
Once targeted, access task array using .task
and use .remove along with filters, to remove that entry from the array
I have console.logged() all the variables and it tallies with the data fields, however the entry is not being removed. What am I doing wrong?
I managed to solve my problem, hopefully this helps someone else
app.delete("/tasks", (req, res) => {
User.findByIdAndUpdate(req.user.id, {$pull: {"task": {text: req.body.text}}}, {safe: true, upsert: true},
function (err, node) {
// console.log here for debugging, if you want
})
})
This successfully erases items based on
req.user.id
"task" : {//whatever your stricter conditions are}
I still don't understand why my earlier attempts failed, but at least this one works.
select reservation_datetime
from LectureReservation
Inner Join Lecture
On LectureReservation.lecture_id = Lecture.id
Where Lecture.mentor_id = 1
This is my query and I want to change it to sequelize like
if (req.params.id) {
LectureReservation
.findAll({
include: [{
model: Lecture,
where: { mentor_id: req.params.id },
}],
attributes: ['reservation_datetime'],
where: {
lecture_id: Lecture.id,
},
this.. I tried it so hard but can't find solution and my postman keep showing me
"name": "SequelizeEagerLoadingError"
this err..
plz help me to translate query to sequelize..!
Sequelize will do _outer join without required = true.
The errors you have received usually is from association problem.
Try set logging :console.log and check the raw query.
I have a mongo collection where I need to combine q regex search with a $text search as well. Using Robo 3t this seems to work fine and I get want I want.
However when trying to get this running in my node app along with mongojs, the query wont work until I remove the $text.
var inRiverCode = req.body.RiverCode
var inSpecies = req.body.Species
var inMonth = req.body.Month
db.rodAvail.find({
//RiverCode: inRiverCode, $text: { $search: "as"}, WeekStartDisplay:
{$regex: inMonth}
$and : [ {$text: { $search: inSpecies }},
{RiverCode: inRiverCode, WeekStartDisplay: {$regex: inMonth}}]
}, function(err, rodAvailList) {
if (err) {
res.send(err);
console.log(err);
return;
}
});
Am I doing something silly like a mistype in my query or is $text search not supported in mongojs.
I did ask on the git page but its very quite so I expected a long wait for a reply.
https://github.com/mafintosh/mongojs/issues/369
I'am trying to build my query using sequelize, in the where clause I need to give the conditional value from my front-end so i did it like this :
getResults(req) {
return parm
.findAll({
attributes: [
sequelize.literal('DISTINCT "id"')
],
where : {
name: req.query.parm.replace(/"/g, '').split(',')
} ,
raw: true
});
}
and it's working!
but now I need to write a subquery including where clause:
something like this :
SELECT tab1.name FROM
(SELECT name FROM "MYTABLE"
WHERE id = (value from the front-end) AND name IN (values from front-end)
) as tab1
Here is what i have tried :
getTest(req) {
if (req.query.parm != null) {
return parm .sequelize.query('SELECT id FROM "table_base" where id = $mid ); ',
{ type: sequelize.QueryTypes.SELECT ,
bind: { mid: [req.query.parm.replace(/"/g, '').split(',')] }} );
}
},
i tried to use raw query and i tested the binding parameters but i get this error when i execute this testing query :
Executing (default): SELECT id FROM "table_base" where id = $1 );
The answer to your question is YES it is indeed possible! SQL can pretty much do anything even if you are using sequelize. If you write the subquery and it doesn't work just post it back here so people can take a look and debug. Thanks
How to write simple query in couchDB for documents of this type
{
_id: q12312312,
_rev: 1-f591846a021c02a6014702f35cc8627c,
mid: e1e410788853b1cfd7820b9092004686,
uid, e1e410788853b1cfd7820b9092000f60
}
I want to get documents by "uid". In SQL that would be done with this command:
SELECT * FROM users_music WHERE uid="1edsae23wsa"
I have a view in CouchDB
`function(doc)
{
if(doc.uid && doc.mid)
{
emit(doc.uid, doc.mid);
}
}`
When I try to access this view from this link http://localhost:5984/user_music/_design/music/_view/musicview?key=e1e410788853b1cfd7820b9092000f60
I get
{"error":"bad_request","reason":"invalid_json"}
Try
http://localhost:5984/user_music/_design/music/_view/musicview?key="e1e410788853b1cfd7820b9092000f60"
Notice the quotes around the key.