データベースにさまざまな行数と列数のテーブルを書き込もうとしています。このために、テーブルヘッダーを保存するCriterion
コレクションと、テーブルデータを保存するOption
コレクションがあります。Meteorメソッドinsert not working(mongodb)
構造は次のようである:Criterion
ため
:
Option
ため{ { "_id" : "hId1", "name" : "Option Name", "tableId" : "tId1" }, { "_id" : "hId2", "name" : "Rent", "score" : 9, "tableId" : "tId1" }, { "_id" : "hId3", "name" : "Surface", "score" : 5, "tableId" : "tId1" }, { "_id" : "hId4", "name" : "Price", "score" : 5, "tableId" : "tId1" }, { "_id" : "hId5", "name" : "CPU", "score" : 5, "tableId" : "tId4" } etc. }
:
{ { "_id" : "id1", "score" : 5, "hId1" : { "value" : "Apt 1" }, "hId2" : { "value" : "800 sqft", "score" : 1 }, "hId3" : { "value" : "$800", "score" : 3 }, etc. "tableId" : "tId1" } { "_id" : "id2", "score" : 5, "hId1" : { "value" : "Apt 2" }, "hId2" : { "value" : "780 sqft", "score" : 10 }, "hId3" : { "value" : "$700", "score" : 3 }, etc. "tableId" : "tId1" } etc. }
Criterion
の最初の行は常に"Option Name"
です。上記のデータについて、"tableId" = "tId1"
とテーブルが(tableId
とheaderId
が鍵である)このように見て終わるだろう:isFirst
がある
/**
* Options are for the rows
*/
export var Option = new Mongo.Collection('option');
/**
* Criteria are the columns
*/
export var Criterion = new Mongo.Collection('criterion');
Meteor.methods({
'comparison.insertRow' (query, headerId, tableId, isFirst) {
check(query, Object);
check(headerId, String);
check(tableId, String);
check(isFirst, Boolean);
if(isFirst){
var data = {};
data._id = headerId;
data.tableId = tableId;
data.name = "Option Name";
Criterion.insert(data);
}
query._id = tableId;
Option.insert(query);
},
});
:
| Option Name | Surface | Price |
| =========== | ======== | ===== |
| Apt 1 | 800 sqft | $800 |
| Apt 2 | 780 sqft | $700 |
私のコードは、この(imports/api/comparison.js
)のように見えますこれが表の最初の行であるかどうかを表すブール値
私のクエリは、このように構成されており(imports/ui/Menu/DataInsert.jsx
):import '../imports/api/comparison.js';
query
をOption
なしに何ら問題は挿入されません:私はserver/main.js
でこれをやっているので、
var query = {};
query.score = // value
// get the header separately
query[headerId] = {
value: //valueH from form
};
// Find the text field via the React ref
for (var i = 1, len = cols.length; i < len; i++) {
query[cols[i]._id] = {
value: //valueV from form,
score: //valueS from form
};
}
私のファイルがサーバー上で利用可能な問題。
data
がCriterion
に挿入されるのはなぜですか(isFirst = true
)?
は私がconsole.log(data)
とconsole.log(query)
を行なったし、それは次のようになります。DB内のデータは次のようになり、一方、
挿入する前に_idフィールドを手動で設定している可能性があります。 Criterion.insertに2番目の引数としてコールバックを追加し、最初の引数としてエラーオブジェクトを受け入れる必要があります。これを使用して、挿入がエラーを投げているかどうかを確認します。 [api](http://docs.meteor.com/api/collections.html#Mongo-Collection-insert)のサーバー**の**部分をお読みください。 – jordanwillis