私はnodejs、mongodb/mongoose、expressでapiを作成して、jsonデータ、より具体的には2つのmongodbコレクション(1:datas 2:produits)でCRUD要求を作成しました。Mongoose APIリクエストとPostmanを使用して空の配列を返しますか?
"datas"コレクションは "User"スキーマにバインドされています。
"produits"コレクションは、 "製品"スキーマにバインドされています。
私の問題がある:「ユーザー」スキーマで
、私は/ produits
コレクションから、製品のIDを救うでしょう場所Productlist
を持っています。
私のユーザー・スキーマは次のようになります。
は//Get module
var mongoose = require('mongoose');
var prodSch = require('../models/productSchema');
var Schema = mongoose.Schema;
//Create user shema
var user = new Schema({
"UIDUser": String,
"IDUser": String,
"UIDACL": String,
"DatasUser": {
"acl_user_id": String,
"prenom": String,
"role": String,
"nom": String,
"pseudo": String,
"email": String
},
"Productlist" : [{ type: Schema.Types.ObjectId, ref: 'prodSch'}],
"Data_Unknown": {}
},
{
collection : 'datas' // define the collection to use
});
//Export schema
module.exports = mongoose.model('userSch', user);
だから私のUser
スキーマはdataschema.js
に保存されます。
私は別のファイルproductSchema.js
product
のスキーマを持っています。
//Get module
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//Create product shema
var Product = new Schema({
"product": String,
"UIDProduct": String,
"HashKey": String,
"EAN13": String,
"UIDReader": String,
"URL": String,
"readers_Label": String,
"devices_Label": String,
"EntryCatalis": [{
"TETITISC": String,
"TEGRDIMG": String,
"TEPETIMG": String,
"TEREFIMG": String,
"LISCEISC": String
}],
"URLHeader": { "Content-Length": Number, "Last-Modified": String},
"ExpireOn": String,
"Data_Unknown": {}
},
{
collection : 'produits' // Define the collection to use
});
// exports schema
module.exports = mongoose.model('prodSch', Product);
私は、ユーザーProductlist
で作成されたすべての製品のIDを保存する必要がある、と私はそれを行うために移入を使用する必要があります。
例:
product{name: tomato, origin: spain, id: 001}
user{fname: john, lname: doe, Productlist: 001} //id of the product, saved
//via populate
私はUser
スキーマと私のProduct
スキーマのCRUD機能を初期化/作成するには、2つの異なるファイルを持っています。
これらのファイルには、投稿、取得、削除、get_by_Idなどのすべての機能があります。 2つのファイルに分かれています。
Productlist
私はスキーマを定義しました。種類とdataschema.js
でproduct
スキーマの参照:私はProductlist
を移入する.post
機能にいくつかのテストを実行しようとしましたdataschema.js
ためCRUD機能を管理routeUser.js
で次に
"Productlist" : [{ type: Schema.Types.ObjectId, ref: 'prodSch'}]
。
例:
.post(function(req, res){
var newData = new userSch();
newData.UIDUser = req.body.UIDUser;
newData.IDUser = req.body.IDUser;
newData.UIDACL = req.body.UIDACL;
newData.DatasUser = req.body.DatasUser;
newData.acl_user_id = req.body.acl_user_id;
newData.prenom = req.body.prenom;
newData.role = req.body.role;
newData.nom = req.body.nom;
newData.pseudo = req.body.pseudo;
newData.email = req.body.email;
newData.Data_Unknown = req.body.Data_Unknown;
newData.save(function(err){
if (err)
res.status(400).send(err);
userSch.find().populate('Productlist').exec(function (err, story) {
if (err) return handleError(err);
console.log('%s', userSch.Productlist);
res.json({message: "data created successfully"});
});
});
});
私はproductSchema.js
のためにCRUD機能を管理ファイルrouteProduct.js
でもテストを試してみました。
これで、user
スキーマのProductlistでプロダクトIDを取得できません。 私は人口に関する多くのチュートリアルを検索しましたが、私の問題の正しいものは見つかりませんでした。
私はこの考えを理解していますが、正しく使用する方法はわかりません。
私のエラーとは何ですか、私のProductlist
に正しく入力する方法を理解するのを助けてください。
編集:
私は自分のコードにいくつかの変更を行った、私はエラーなしで私の要求を行うことができますが、私の移入はまだ動作しません。これは私が変更したラインである
:そのコードで
.post(function(req, res){
var newData = new prodSch();
newData.product = req.body.product;
newData.UIDProduct = req.body.UIDProduct;
newData.HashKey = req.body.HashKey;
newData.EAN13 = req.body.EAN13;
newData.UIDReader = req.body.UIDReader;
newData.URL = req.body.URL;
newData.readers_Label = req.body.readers_Label;
newData.devices_Label = req.body.devices_Label;
newData.EntryCatalis = req.body.EntryCatalis;
newData.TETITISC = req.body.TETITISC;
newData.TEGRDIMG = req.body.TEGRDIMG;
newData.TEPETIMG = req.body.TEPETIMG;
newData.TEREFIMG = req.body.TEREFIMG;
newData.LISCEISC = req.body.LISCEISC;
newData.URLHeader = req.body.URLHeader;
newData['Content-Length'] = req.body['Content-Length'];
newData['Last-Modified'] = req.body['Last-Modified'];
newData.ExpireOn = req.body.ExpireOn;
newData.Data_Unknown = req.body.Data_Unknown;
newData.populate('userSch').save(function(err){ // <--- The line
if (err)
res.send(err);
res.json({message: "data created successfully"});
});
});
を、私は、CRUDのユーザーまたは製品ができますが、それは私のユーザー・スキーマ内の私Productlistを移入しません。
オンラインスペルチェッカーで質問ベースを編集できますか?私はこれが迷惑になる可能性があることを知っている、あなたはすでにこの質問を作成するいくつかの時間を過ごしている。しかし、読むのは難しいです。 –
あなたはより具体的になることができますか?申し訳ありませんがあなたの質問を正確に理解していない私はすべてを変更するか?それとも質問のタイトルだけ? – amalrik
@DragandDrop私の編集が正しいと思っています、何かが間違っているなら、もう一度教えてください、ありがとう。 – amalrik