Vue.js 加载到Sails应用程序后访问Vue中的对象属性
当SailsJS应用程序加载一个页面时,我让它从url中提取Vue.js 加载到Sails应用程序后访问Vue中的对象属性,vue.js,sails.js,Vue.js,Sails.js,当SailsJS应用程序加载一个页面时,我让它从url中提取id参数,并从数据库中加载一个配方。recipe对象正确地登录到控制台,因此我确信它正在加载,但没有任何Vue变量正在呈现 我正在加载此控制器操作中的数据: //api/controllers/recipes/view-single-recipe.js module.exports={ friendlyName:“查看单一配方”, 描述:'显示“单一配方”页面', 出口:{ 成功:{ viewTemplatePath:“页面/配方/单
id
参数,并从数据库中加载一个配方。recipe对象正确地登录到控制台,因此我确信它正在加载,但没有任何Vue变量正在呈现
我正在加载此控制器操作中的数据:
//api/controllers/recipes/view-single-recipe.js
module.exports={
friendlyName:“查看单一配方”,
描述:'显示“单一配方”页面',
出口:{
成功:{
viewTemplatePath:“页面/配方/单一配方”
}
},
fn:异步函数(输入、输出){
const recipe=wait recipe.find({id:this.req.params.id}).populate('components'))
console.log(recipe)//正确记录数据
返回成功({
食谱:食谱
});
}
};代码>答案是当使用find()
查询时,返回的结果是一个数组。因此,如果只有一个结果,则需要在数组中的第一个结果处访问它[0]
//api/controllers/recipes/view-single-recipe.js
module.exports={
// ...
fn:异步函数(输入、输出){
const recipe=wait recipe.find({id:this.req.params.id}).populate('components'))
console.log(recipe)//正确记录数据
返回成功({
配方:配方[0]
});
}
};代码>您应该使用
还请注意,fn中的输入
变量未被使用。
如果记录不存在,则需要一些处理程序。我不知道SailsJS,但在fn中,您调用exits.success作为函数,但它是模块中的一个对象。exports对象,它是否与错误有关?
// api/controllers/recipes/view-single-recipe.js
module.exports = {
friendlyName: 'View single recipe',
description: 'Display "Single recipe" page.',
exits: {
success: {
viewTemplatePath: 'pages/recipes/single-recipe'
}
},
fn: async function (inputs, exits) {
const recipe = await Recipe.findOne({
id: this.req.params.id
}).populate('ingredients')
return exits.success({ recipe: recipe });
}
};