gpt4 book ai didi

node.js - Mongoose populate() 返回空数组

转载 作者:行者123 更新时间:2023-12-03 12:13:16 29 4
gpt4 key购买 nike

我想通过 clientSchema 中的属性 'token' 查询子文档数组。但我无法填充子文档数组。它总是返回空值。

这是我尝试过的

var performAuthAsync = promise.promisify(performAuth);

var response = {};
performAuthAsync(req).then(function (client) {

sendStatus(res, 200, { "success": "true", "value": client });

}).catch(ApiError, function (e) {
response.error = "true";
response.message = e.message;
if (e.message == "Invalid Authorization" || e.message == "Unauthorized access") {
console.log(e.message);
sendStatus(res, 401, response, req.query.type);
}
else {
sendStatus(res, 500, response, req.query.type);
}

});

PerformAuth 方法
function performAuth(req, callback) {
try {
var authHeader = req.headers.authorization;
console.log(authHeader);
//error in req format
if (!authHeader || !authHeader.startsWith("Basic ")) {
console.log("inside fail authheader");
return callback(new ApiError("Invalid Authorization"));
}

authHeader = authHeader.replace("Basic ", "");
authHeader = Buffer.from(authHeader, 'base64').toString('ascii');

console.log(authHeader);

//temporary populate check
clientApp.findOne({}).populate({

path: 'appClients',
model: 'TClient'

}).exec(function (error, apps) {
console.log("populated apps check " + apps); //object containing empty array

//{ _id: 5987099f2cb916a0de80f067,
// appSecret: 'THisIsSecret',
// appId: 'W5ikGw16dQjgWm8bGjqdAwi1IDR2XibD3XESYokH',
// appClients: [] }

// mongo console output
// { "_id" : ObjectId ("5987099f2cb916a0de80f067"),
// "appSecret" : "THisIsSecret",
// "appId" : "W5ikGw16dQjgWm8bGjqdAwi1IDR2XibD3XESYokH",
// "appClients" : [ ObjectId("59881a64dbab536016e7f970") ], "__v" : 0 }
});

clientApp.findOne({}).populate('appClients').findOne({
'appClients.token': authHeader
}).exec(function (error, client) {

if (error) {
console.log("inside dberror");
console.error(error);
return callback(error, null);
}

if (!client) {
return callback(new ApiError("Unauthorized access"), null);
}

return callback(client);

});
}
catch (exception) {
console.log("inside exception");
console.error(exception);
return callback(exception, null);
}

}

Clientapp 和客户端模式:(它们在不同的文件中)
var appSchema = new Schema({
appId: {
type: String,
required: true,
unique: true
},
appSecret: {
type: String,
required: true,
unique: true
},
appClients: [{ type: Schema.Types.ObjectId, ref: 'TClient' }],
createdAt: Date,
modifiedAt: Date
});

// model
var clientApp = mongoose.model('ClientApp', appSchema);


var clientSchema = new Schema({
clientId: {
type: String,
required: true,
unique: true
},
info: {
type: String,
required: true,
},
token: {
type: String,
required: true,
unique: true
},
createdAt: Date,
modifiedAt: Date
});

// model
var tclient = mongoose.model('TClient', clientSchema);

我做错了什么?任何帮助表示赞赏

最佳答案

findOne()函数只返回一个文档。之后, populate 方法填充子文档值。但是下一个findOne()不工作,所以你得到一个 null回复。

你可以试试这个:

async function performAuth(){
let authHeader = authHeader.replace("Basic ", "");
authHeader = Buffer.from(authHeader, 'base64').toString('ascii');
const clientApp = await clientApp.find({})
.populate('appClients')
.map(data => data.find(item=>item.appClients.token === authHeader).exec();
}

执行的操作
  • 获取所有客户端应用
  • 填充 appClients
  • 查找 token 与 authHeader
  • 匹配的客户端应用程序

    关于node.js - Mongoose populate() 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45541030/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com