我用 nodejs、mongodb/mongoose 和 express 创建了一个 api,使 CRUD 请求与 json 数据交互,更具体地说,与两个 mongodb 集合(1:数据 2:产品)。
“数据”集合绑定(bind)到“用户”模式。
“产品”集合绑定(bind)到“产品”模式。
我的问题是:
在“用户”模式中,我有一个地方 Productlist
,它将从 produits
集合中获取/保存产品的 ID。
我的用户架构如下所示:
//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,我需要使用 populate 来做到这一点。
例如:
product{name: tomato, origin: spain, id: 001}
user{fname: john, lname: doe, Productlist: 001} //id of the product, saved
//via populate
我有两个不同的文件来创建/初始化我的 User
模式和我的 Product
模式的 CRUD 功能。
在这些文件中,我拥有发布、获取、删除、get_by_Id 等的所有功能... 但分为两个文件。
因此,为了填充 Productlist
,我定义了架构。 dataschema.js
中 product
模式的类型和引用:
"Productlist" : [{ type: Schema.Types.ObjectId, ref: 'prodSch'}]
然后在管理 dataschema.js
的 CRUD 函数的 routeUser.js
中,我尝试在 .post
函数中做一些测试以填充 Productlist
。
例如:
.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
上使用 populate?
编辑:
我对我的代码做了一些更改,我可以毫无错误地提出我的请求,但我的 Populate 仍然不起作用。
这是我修改过的行:
.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,但它不会在我的用户模式中填充我的产品列表。
最佳答案
您必须了解 .populate()
仅在查询期间有用,在保存/更新/创建期间没有用。所以你的第二个例子没有做任何有用的事情:
newData.populate('userSch').save(...)
为了填充用户的 Productlist
条目,您首先需要一个 prodSch
文档。
假设您有一个现有用户,并且您想要将产品添加到他们的产品列表中。您首先创建产品并保存它:
let product = new prodSch(req.body);
product.save(function(err, product) { ...
然后,您想将其添加到现有用户的产品列表中,我们将通过一个名为 userId
的变量来识别该用户。
我们首先找到用户:
userSch.findById(userId, function(err, user) { ...
然后我们将产品添加到他们的产品列表中:
user.Productlist.push(product);
然后我们保存用户:
user.save(function(err, user) { ...
一切结合起来(为了简洁省略了错误和存在检查,但一定要自己添加!):
let product = new prodSch(req.body);
product.save(function(err, product) {
userSch.findById(userId, function(err, user) {
user.Productlist.push(product);
user.save(function(err, user) {
...done...
});
});
});
除了findById/push/save
,你还可以使用findByIdAndUpdate
:
product.save(function(err, product) {
userSch.findByIdAndUpdate(userId, {
$push : { Productlist : product._id }
}, {
new : true // return the updated document, if you want
}, function(err, user) {
...done...
});
});
然后,查询用户并填充他们拥有的产品列表:
userSch.findById(userId).populate('Productlist').exec(function(err, user) {
...
});
值得阅读并完全理解有关人口的文档:http://mongoosejs.com/docs/populate.html
关于javascript - Mongoose 填充API请求和 postman ,返回空数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44153676/