help | Adding an array to the user's db
-
good evening,
What I actually want to do is add an array to the user's db,
As each user has an array with the posts he wrote, etc.
I want to know how to add such an array to a specific user.
For example array:{shop: [ {name: 'a', phone: 1}, {name: 'a', phone: 1} ]}
That's it, I think it's a very simple technical matter of db.setObject
But I don't want to do anything stupid, afraid that my mongo will crash...I would appreciate your help, it's quite urgent, thanks in advance!
-
To set it
const uid = 1; const shop = [ {name: 'a', phone: 1}, {name: 'a', phone: 1} ]; await db.setObject(`user:${uid}`, { shop: JSON.stringify(shop) });
To load it
const shop = JSON.parse(await db.getObjectField(`user:${uid}`, 'shop'));
I didn't add the extra code to check if the shop field exist and/or it's valid, you can wrap it in a try/catch block etc.
-
It also works on socket when loading Require of db right?
I am not sure what you mean by this.
If you want to add a new item you need to load it and use array.push and then save that back.
const shop = JSON.parse(await db.getObjectField(`user:${uid}`, 'shop')); shop.push({name: 'a', phone: 1}); await db.setObject(`user:${uid}`, { shop: JSON.stringify(shop) });
-
I'm just now trying to develop some kind of system through a socket on the server, if you could help me regardless. Thank you very much!
I don't know how to perform a callback in a socket, for example:
I loaded a socket on the client side, and on the server side a callback of a command on the client side is defined - like app.alert, so when I load the socket on the client side, it will execute app.alert for me - as defined in the extension of the socket on the server side.
This is exactly what zany is looking for, only I don't exactly know how to set it up, I would really appreciate it if you could help me with this! -
Sounds like you are trying to send an event from server to client. You can do that with
socket.emit('someEvent', data);
on the server side. And on the client side you listen to that event withsocket.on('someEvent', function (data) { });
.There are plently of examples of this in nodebb if you search for socket.emit/on. NodeBB uses socket.io so their documentation is also useful. https://socket.io/docs/v4/emitting-events/
-
In that case you can simply use
socket.emit
on the client side and respond to that with data from the server side. You can see how it's done in a plugin like https://github.com/NodeBB/nodebb-plugin-category-notifications. Client sends here and server responds here -
Thanks!
Apparently I was looking for this command:throw new Error('נתונים שגויים');
But is this the only way to send events to the client?
If I write console.log() on the server side, does it print to the console on the client side?throw console.log()
-
throw new Error('testing')
is used for alerting the client side about an error. The reason you are seeing that on the client side is because the server side is catching the error and sending the error message as a string to the client. That happens here.If you want to send custom messages to the client you can just return them from your socket function on the server. Here is simple example.
// on the client socket.emit('plugins.myPlugin.myCustomEvent', { data: 1 }, function (err, returnData) { if (err) { return alerts.error(err); // if server throws an error it will be displayed } console.log('server returned', returnData); }); // on the server const sockets = require.main.require('./src/socket.io/plugins'); sockets.myPlugin.myCustomEvent = async function (socket, data) { console.log('got data from client', data); return { msg: 'foo' }; };
-
@baris Don't know why, but it doesn't work...
Server side:
SocketPlugins.myPlugin = function (socket, data) { console.log('got data from client', data); return { msg: 'foo' }; };
I ran client side:
socket.emit('admin.plugins.testin', { data: 1 }, function (err, returnData) { if (err) { return app.alertError(err); // if server throws an error it will be displayed } console.log('server returned', returnData); });
And this is the response:
undefined
-
@LEVI-HAVIV the name you use in the socket.emit needs to match the name of the function on the server side. For a socket function in the admin namespace the below sample should work
// on the client socket.emit('admin.plugins.myPlugin.myCustomEvent', { data: 1 }, function (err, returnData) { if (err) { return alerts.error(err); // if server throws an error it will be displayed } console.log('server returned', returnData); }); // on the server const sockets = require.main.require('./src/socket.io/admin/plugins'); sockets.myPlugin = {}; sockets.myPlugin.myCustomEvent = async function (socket, data) { console.log('got data from client', data); return { msg: 'foo' }; };
Keep in mind when a socket event starts with
admin.
only users who are administrators will be able to call this. -
Oh! Thank you very much!
I didn't realize that was the problem...
In any case, is it possible to transmit js commands to the client? And not text or arrays?
js example:return console.log('test');
Thank you very much for the help so far
-
I don't think you can send javascript functions, you would have to send a string and then run a command based on that string. A sample below
// on the client socket.emit('admin.plugins.myPlugin.myCustomEvent', { data: 1 }, function (err, returnData) { if (err) { return alerts.error(err); // if server throws an error it will be displayed } if (returnData.log) { console.log(returnData.log); } }); // on the server const sockets = require.main.require('./src/socket.io/admin/plugins'); sockets.myPlugin = {}; sockets.myPlugin.myCustomEvent = async function (socket, data) { console.log('got data from client', data); return { log: 'this message will be logged client side' }; };