7

Vấn đề tôi có liên quan đến đoạn trích sau đây từ official documentation:Xử lý trong Sails.js

Note If any connection to an adapter is used by a model, then all connections to that adapter will be loaded on sails.lift, whether or not models are actually using them. In the example above, if a model was configured to use the localMysql connection, then both localMysql and remoteMysql would attempt to connect at run time. It is therefore good practice to split your connection configurations up by environment and save them to the appropriate environment-specific config files, or else comment out any connections that you don't want active.

Làm thế nào bạn có thể cấu hình kết nối cho một máy chủ sản xuất?

connections.js My tập tin trông như thế này:

module.exports.connections = { 

    mongoDev: { 
    adapter: 'sails-mongo', 
    host: 'localhost', 
    port: 27017, 
    user: 'username', 
    password: 'password', 
    database: 'database' 
    }, 

    mongoLive: { 
    adapter: 'sails-mongo', 
    host: 'host.mongolab.com', 
    port: 31681, 
    user: 'user', 
    password: 'password', 
    database: 'database' 
    } 
}; 

Và trong tập tin cấu hình môi trường của tôi, tôi đã có:

development.js

module.exports = { 
    models: { 
    connection: 'mongoDev' 
    }  
}; 

production.js

module.exports = { 
    models: { 
    connection: 'mongoLive' 
    }, 
    port: 3000, 
}; 

Điều này hoạt động trên máy cục bộ của tôi, vì máy chủ cơ sở dữ liệu sản xuất nằm trên máy chủ bên ngoài. Trên môi trường sản xuất tôi nhận được lỗi sau:

[Error: failed to connect to [localhost:27017]] 

Nó hoạt động nếu tôi loại bỏ các đối tượng mongoDev từ đối tượng kết nối của tôi.

Tôi cũng đã thử sử dụng adaptors.js, nhưng điều này chỉ dẫn đến một số lỗi không dùng nữa.

$ sails -v 
info: v0.9.9 

tôi nhận được một cái gì đó khác nhau khi chạy sails lift:

info: Sails   
info: v0.10.5 

Trả lời

12

Bạn muốn lưu các định nghĩa kết nối thực tế trong hai development.js hoặc production.js và loại bỏ chúng khỏi connections.js. Đó là một chút không trực quan.

development.js

module.exports = { 
    connections : { 
    mongoDev: { 
     adapter: 'sails-mongo', 
     host: 'localhost', 
     port: 27017, 
     user: 'username', 
     password: 'password', 
     database: 'database' 
    } 
    }, 
    models: { 
    connection: 'mongoDev' 
    }  
}; 

production.js

module.exports = { 
    connections : { 
    mongoLive: { 
     adapter: 'sails-mongo', 
     host: 'host.mongolab.com', 
     port: 31681, 
     user: 'user', 
     password: 'password', 
     database: 'database' 
    } 
    }, 
    models: { 
    connection: 'mongoLive' 
    }, 
    port: 3000, 
}; 
+0

này dường như không làm việc cho tôi. Bạn có biết tại sao bạn phải làm điều này với thiết lập môi trường? Có cái gì đó buộc nó trở lại địa phương? – Scott

+1

câu trả lời này có các liên kết về lý do tại sao điều này * là * một vấn đề http://stackoverflow.com/questions/28658299/sailsjs-still-uses-default-database-after-changing-it-to-mongodb/28664845#28664845 – Meeker

Các vấn đề liên quan