meteor-mssql icon indicating copy to clipboard operation
meteor-mssql copied to clipboard

How to set Meteor.Settings

Open emgee3 opened this issue 9 years ago • 8 comments

(Copied from a different, unrelated issue.)

@yanxiaoyan:

Hi there,
I am a learner,
【meteor.settings】Should be how to set it?

@batjko:

You create a settings.json in your project folder, with the settings object inside. 
Then you start meteor with this parameter: meteor --settings settings.json

Then the settings will be available to your Meteor app as Meteor.settings, and this package automatically uses it to establish a database connection.

@yanxiaoyan:

I created a settings.json in my project,but can not connect to the database,I think it should be the configuration parameter is not right, server this add port number?

emgee3 avatar Sep 09 '15 03:09 emgee3

@yanxiaoyan

Your settings.json file does not need to be in the project, but you do need to reference this file when launching Meteor. Example:

$ meteor --settings /path/to/settings.json

You can see the format for the database settings object here: https://github.com/patriksimek/node-mssql#configuration-1

emgee3 avatar Sep 09 '15 03:09 emgee3

@emgee3 How can I get to the data? API is not very clear to me. I hope to give me a look,If you have a small case, I hope you can send me a copy. very grateful.

WangYans avatar Sep 09 '15 04:09 WangYans

The Sql object is only exposed on the server. So if you have the following code on the server:

if (Meteor.isServer) {
    Meteor.startup(function () {
        Sql.q("select 1 as num", function (err, res) {
            console.log(res);
        });
    });
}

You should see something like:

I20150908-21:30:48.708(-7)? { num: 1 }

logged to the console.

Note: I didn't test this code, you might need to put a delay for the driver to connect to the database before you can run a query.

emgee3 avatar Sep 09 '15 04:09 emgee3

@emgee3 I still don't understand, this can only be written in the server?

WangYans avatar Sep 09 '15 06:09 WangYans

Correct, the Sql object is server-only. You do not want clients able to write arbitrary SQL code. So if you need to get the results of a query to the client, wrap it in a Meteor Method, or add code to insert into a collection. See http://docs.meteor.com/#/full/meteor_methods and http://docs.meteor.com/#/full/meteor_publish (relevant part starts with "Alternatively, a publish function can directly control its published record set by calling the...")

emgee3 avatar Sep 09 '15 06:09 emgee3

@emgee3 I'm not quite understand,how should I add code to insert into a collection

WangYans avatar Sep 09 '15 07:09 WangYans

@emgee3 I put it in the method, but there is still a mistake, how to change this? very grateful.

image image image image

WangYans avatar Sep 09 '15 08:09 WangYans

So what you're running into isn't really a emgee:mssql problem, there's a bit more you have to learn about how async works in Meteor. Your template helper isn't going to wait until the async Meteor.call completes, so your query results aren't going to be available to the template. There are different strategies to handle this, and I can't really teach a Meteor class in this issue.

What I suggest is Discover Meteor, the chapter on Advanced Reactivity goes over this general subject.

A couple other hints in your code -- you should prepare the SQL statement outside the method. You only need to prepare the statement once, then you can use multiple times. Also, your try-catch should contain the query itself.

emgee3 avatar Sep 09 '15 16:09 emgee3