I have this code inside of a route and when I run the debugger, the theDocu2.save(); function is being run and then the socket emit but I never see the $set run which sets the new mongoose values. Any reason for this? Also, in the debugger, values like bidTime and bidAmount are equal to the previous values that were set the last time this route was run.
User.findByIdAndUpdate(req.session.passport.user, {
$set: {
bidDetails: {
bidAmount: req.body.bidAmount,
bidLocation: 'Market Square',
bidTime: moment().format()
}
}
}, function(err, theDocu2) {
if (err) {
return console.log(err);
}
theDocu2.save();
io.to(theDocu2.email).emit('activeBid', {
activeBid: req.body.bidAmount,
bidTime: theDocu2.bidDetails.bidTime
});
});
Related
I have a problem with updating some values in Meteor app on client-side. I'm trying to understand how ReactiveVar works.
When I use find() method on collection on client-side the site updates immediately each time I change something. I want to achieve the same effect using ReactiveVar and server-side Method. So the code below works correctly for me:
// Client
Template.body.onCreated(function appBodyOnCreated() {
this.subscribe('activities');
}
Template.body.helpers({
getCounter() {
return Activities.find({
editorId: Meteor.userId(),
'referredObject.type': 'LIST'
}).count();
}
});
But when I try to achieve the same effect with server-side Method it doesn't work correctly. Code below updates variable only once. If I want to get current value I need to refresh the page.
// Server
Meteor.methods({'activitiesCreateCount'(userId, objectType) {
check(userId, String);
check(objectType, String);
return Activities.find({
editorId: userId,
'referredObject.type': objectType
}).count();
}
});
// Client
Template.body.onCreated(function appBodyOnCreated() {
this.subscribe('activities');
this.activitiesAmount = new ReactiveVar(false);
}
Template.body.helpers({
getCounter() {
var tempInstance = Template.instance();
Meteor.call('activitiesCreateCount', Meteor.userId(), 'TODO', function(err, response) {
tempInstance.activitiesAmount.set(response);
});
return Template.instance().activitiesAmount.get();
}
});
How I can improve my code if I want always have a current value of the variable (like in the first client-side only example)?
Try to move Meteor.callto Template.body.onCreated
Something like this
// Server
Meteor.methods({'activitiesCreateCount'(userId, objectType) {
check(userId, String);
check(objectType, String);
return Activities.find({
editorId: userId,
'referredObject.type': objectType
}).count();
}
});
// Client
Template.body.onCreated(function appBodyOnCreated() {
self = this;
this.activitiesAmount = new ReactiveVar(false);
Meteor.call('activitiesCreateCount', Meteor.userId(), 'TODO', function(err, response) {
self.activitiesAmount.set(response);
});
}
Template.body.helpers({
getCounter() {
return Template.instance().activitiesAmount.get();
}
});
I'm pretty new to Mongodb and have so far successfully used Find, Insert, Update methods. However, with Delete function I am not able to access WriteResult
Insert (Works)
productCollection.insert(newProduct, function (err, result) {
callBack(err, { message: result["insertedCount"] + ' product created successfully.' });
});
Find (Works)
productCollection.find({}).toArray(function (err, docs) {
callBack(err, { product: docs });
});
Delete (Has Issues)
productCollection.remove({ id: pId }, { justOne: 1 }, function (err, result) {
callBack(err, { message: result});
});
Here when I return {message: result} I get
{
"message": {
"ok": 1,
"n": 0
}
}
But I want to actually read "n" from Result to show no of documents deleted
Tried following
{ message: result["n"] }
{ message: result["nRemoved"] }
But in both cases it returns empty object {}.
According to the 2.0 version of Node.js MongoDB Driver API, remove() method is deprecated, you can use removeOne() method instead:
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#remove
In order to receive number of documents that were removed, you need to use safe mode to ensure removal of documents. To do this, specify write concern by passing {w: 1} to removeOne() function:
productCollection.removeOne({ _id: pId }, { w:1 }, function(err, r) {
// number of records removed: r.result.n
callBack(err, { message: r });
});
Hope this helps.
Thanks Yelizaveta for pointing out the deprecated method. However in my case, following worked
productCollection.removeOne({ id: pId }, { w: 1 }, function (err, r) {
callBack(err, { message: r.result["n"]});
});
I could not get r.result.n working instead r.result["n"] worked, which I don't understand.
I am implementing the tutorial on the mean stack https://www.youtube.com/watch?v=AEE7DY2AYvI
I am adding a delete feature to remove items from the database on a button click
My client side controller has the following 2 functions to add to db and remove
$scope.createMeetup = function() {
var meetup = new Meetup();
meetup.name = $scope.meetupName;
meetup.$save(function (result) {
$scope.meetups.push(result);
$scope.meetupName = '';
});
}
$scope.deleteMeetup = function() {
item = $scope.meetups[0];
console.log("deleting meetup: " + item["name"]);
Meetup.delete(item);
scope.meetups.shift();
}
My server side has the following code
module.exports.create = function (req, res) {
var meetup = new Meetup(req.body);
meetup.save(function (err, result) {
res.json(result);
});
}
module.exports.remove = function(req, res) {
console.log("GOING TO REMOVE!!!");
console.log(req.query);
item = req.query;
Meetup.remove(item, function (err, results) {
console.log("done");
console.log(err);
console.log(results);
});
}
When I run my code and if I delete an already loaded item in the list, it is removed from Mongodb just fine. But if I add an item to the list and I do not refresh the page, it results in an error at my server that appears as
GOING TO REMOVE!!!
{ '$resolved': 'true',
__v: '0',
_id: '54ec04e70398fab504085178',
name: 'j' }
done
{ [MongoError: unknown top level operator: $resolved]
name: 'MongoError',
code: 2,
err: 'unknown top level operator: $resolved' }
null
I if I refresh the page, the it gets deleted fine. But if I added the entry, angular seems to be adding a new variable $resolved. Why is that happening?
Also another question, What is the proper way to call delete? I call it now but I am not able to put a callback. I want a callback which returns and then I shift the list of items. I tried adding a callback but the code never reaches it.
ie I tried the following
/*
Meetup.delete(item, function () {
console.log("In callback!!");
console.log(returnValue);
console.log(responseHeaders);
$scope.meetups.splice(item);
});
*/
/*Meetup.delete(item,
function (returnValue, responseHeaders) {
console.log("In callback!!");
console.log(returnValue);
console.log(responseHeaders);
$scope.meetups.splice(item);
},
function (httpResponse){
// error handling here
console.log("Need to handle errors");
});
*/
I am very new to node and am confused. Any help is very, very appreciated
Looks like it possible to call item.delete instead of Meetup.delete(item). You can call same methods on model instance. It prevent sending angular properties to server.
But better to make a rest API with delete method
DELETE /meetups/:id
and send just a _id
Meetup.remove({id: item._id});
I'm looking for a way to determine if Meteor.user() is set in a function that can be called both from the server and client side, without raising an error when it is not.
In my specific case I use Meteor server's startup function to create some dummy data if none is set. Furthermore I use the Collection2-package's autoValue -functions to create some default attributes based on the currently logged in user's profile, if they are available.
So I have this in server-only code:
Meteor.startup(function() {
if (Tags.find().fetch().length === 0) {
Tags.insert({name: "Default tag"});
}
});
And in Tags-collection's schema:
creatorName: {
type: String,
optional: true,
autoValue: function() {
if (Meteor.user() && Meteor.user().profile.name)
return Meteor.user().profile.name;
return undefined;
}
}
Now when starting the server, if no tags exist, an error is thrown: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
So in other words calling Meteor.user() on the server startup throws an error instead of returning undefined or null or something. Is there a way to determine whether it will do so prior to calling it?
I cannot solve this simply by wrapping the call with if (Meteor.isServer) within the autoValue function, as the autoValue functions are normally called from server side even when invoked by the user, and in these cases everything in my code works fine.
Note that this is related to How to get Meteor.user() to return on the server side?, but that does not address checking if Meteor.user() is available in cases where calling it might or might not result in an error.
On the server, Meteor.users can only be invoked within the context of a method. So it makes sense that it won't work in Meteor.startup. The warning message is, unfortunately, not very helpful. You have two options:
try/catch
You can modify your autoValue to catch the error if it's called from the wrong context:
autoValue: function() {
try {
var name = Meteor.user().profile.name;
return name;
} catch (_error) {
return undefined;
}
}
I suppose this makes sense if undefined is an acceptable name in your dummy data.
Skip generating automatic values
Because you know this autoValue will always fail (and even if it didn't, it won't add a useful value), you could skip generating automatic values for those inserts. If you need a real name for the creator, you could pick a random value from your existing database (assuming you had already populated some users).
Been stuck with this for two days, this is what finally got mine working:
Solution: Use a server-side session to get the userId to prevent
"Meteor.userId can only be invoked in method calls. Use this.userId in publish functions."
error since using this.userId returns null.
lib/schemas/schema_doc.js
//automatically appended to other schemas to prevent repetition
Schemas.Doc = new SimpleSchema({
createdBy: {
type: String,
autoValue: function () {
var userId = '';
try {
userId = Meteor.userId();
} catch (error) {
if (is.existy(ServerSession.get('documentOwner'))) {
userId = ServerSession.get('documentOwner');
} else {
userId = 'undefined';
}
}
if (this.isInsert) {
return userId;
} else if (this.isUpsert) {
return {$setOnInsert: userId};
} else {
this.unset();
}
},
denyUpdate: true
},
// Force value to be current date (on server) upon insert
// and prevent updates thereafter.
createdAt: {
type: Date,
autoValue: function () {
if (this.isInsert) {
return new Date;
} else if (this.isUpsert) {
return {$setOnInsert: new Date};
} else {
this.unset();
}
},
denyUpdate: true
},
//other fields here...
});
server/methods.js
Meteor.methods({
createPlant: function () {
ServerSession.set('documentOwner', documentOwner);
var insertFieldOptions = {
'name' : name,
'type' : type
};
Plants.insert(insertFieldOptions);
},
//other methods here...
});
Note that I'm using the ff:
https://github.com/matteodem/meteor-server-session/ (for
ServerSession)
http://arasatasaygin.github.io/is.js/ (for is.existy)
So I'm listening for an event with socket.io, once that fires I'm trying to update a record to a new value.
socket.on('contentEdited', function (newContent) {
collection.update(
{ '_id' : ObjectId("5279262e74d92da751eb2b8e") },
{ $set: {
'content': newContent
}
}
), function (err, result) {
if (err) throw err;
console.log(result)
};
});
The syntax works in the shell, but throws the following error in node when the event fires:
Error: Cannot use a writeConcern without a provided callback
I tried adding an function at the end afterwards for basic error checking, but I'm not sure how to provide a callback in the way mongo expects.
Still kinda new to this, thanks
I think your problem is that the callback function needs to be inside the update function call instead of outside it. The format for the nodejs MongoDB driver can be found here: http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#update
So it should look like this:
collection.update(
{ '_id' : ObjectId("5279262e74d92da751eb2b8e") },
{ $set: { 'content': newContent } },
function (err, result) {
if (err) throw err;
console.log(result);
})
Note that the parentheses has moved after the callback function.
You could also set the write concern to "unacknowledged" instead of "acknowledged."
The MongoDB concept of "Write Concerns" determines how certain you want to be that MongoDB successfully wrote to the DB. The lowest level of write concern, "Unacknowledged" just writes data to the server and doesn't wait to response. This used to be the default, but now the default is to wait for MongoDB to acknowledge the write.
You can learn more about write concerns here:
http://docs.mongodb.org/manual/core/write-concern/
To set the write concern to unacknowledged, add the option {w: 0}:
collection.update(
{ '_id' : ObjectId("5279262e74d92da751eb2b8e") },
{ $set: { 'content': newContent } },
{ w : 0 });
yes. maybe you have the wrong syntax. and this might make it even better
socket.on('contentEdited', function (newContent) {
collection.update(
{ '_id' : ObjectId("5279262e74d92da751eb2b8e") },
{ $set:
{ 'content': newContent }
},
{returnOriginal : false},
function (err, result) {
if (err) throw err;
console.log(result);
});
})