sequelize多条件_在服务器接收小程序的request请求,并对数据库进行多条件查询...
发布日期:2021-06-24 16:21:11 浏览次数:2 分类:技术文章

本文共 3087 字,大约阅读时间需要 10 分钟。

1. nodejs接收小程序客户端的请求数据

(1) 在之前的《微信小程序与服务器的交互原理》已经介绍了微信小程序与服务器交互的基本过程和代码,以下是小程序客户端向服务器请求的接口代码:

wx.request({

url: 'https://www.joyitsai.cn/weapp/checkArea',

// 请求服务器时携带的请求数据

data: {

schoolArea: school_area,

Grade: grade,

checkWay: checkWay,

keyWord: keyWord

},

method: 'GET',

// 携带的参数会以url格式传到服务器,信息头我们设置为url编码,utf8编码

header: {

'content-type': 'application/x-www-form-urlencoded;charset=utf-8'

},

// 提取响应数据

success: function (res) {

var data = res.data.data;

console.log(data);

}, // success: function(){}

fail: function (res) {

console.log('请求数据失败');

}

});

}

(2) 我们在服务器端的路由函数中,接收小程序端发送的URL请求和携带参数:

我们是通过Koa框架的Context对象来接收request和返回response的。详细内容请参见《Koa中文文档》

Koa Context 将 node 的 request 和 response 对象封装到单个对象中,为编写 Web 应用程序和 API 提供了许多有用的方法。 这些操作在 HTTP 服务器开发中频繁使用,它们被添加到此级别而不是更高级别的框架,这将强制中间件重新实现此通用功能。

每个请求都将创建一个 Context,并在中间件中作为接收器引用,或者 ctx 标识符,如以下代码片段所示:

app.use(async ctx => {

ctx; // 这是 Context

ctx.request; // 这是 koa Request

ctx.response; // 这是 koa Response

});

为方便起见许多上下文的访问器和方法直接委托给它们的 ctx.request或 ctx.response ,不然的话它们是相同的。 例如 ctx.type 和 ctx.length 委托给 response 对象,ctx.path 和 ctx.method 委托给 request。

request.querystring(ctx.querystring): 根据 ? 获取原始查询字符串

request.query(ctx.query): 获取解析的查询字符串, 当没有查询字符串时,返回一个空对象。

例如 http://xxxxx/?color=blue&size=small,通过ctx.query获取的解析数据对象为:

{

color: 'blue',

size: 'small'

}

以下是路由接口js脚本中接收微信小程序端request请求的代码:

module.exports = async(ctx) => {

let url=ctx.url; //获取url

console.log('Here is the url of ctx:');

console.log(url);

// 从context中直接获取URL携带的数据

let ctx_query = ctx.query; //query返回格式化的对象

console.log('Here is the query of the ctx:');

console.log(ctx_query);

let req_querystring=request.querystring; //querystring返回原字符串。

var schoolArea = ctx.query.schoolArea;

var grade = ctx.query.Grade;

var checkway = ctx.query.checkWay;

var keyword = ctx.query.keyWord;

console.log('此次request请求携带的数据为:'+ schoolArea + ', ' + grade + ', ' + checkway + ', ' + keyword);

}

2. 通过nodejs的sequelize框架向数据库进行数据查询

在上面的过程中,我们已经获取到了来自微信小程序端的resuqest请求所携带的数据:schoolArea,grade,checkway,keyword

在前面的文章中《微信小程序与服务器的交互原理》,已经介绍过了如何通过sequelize来连接mysql数据库和创建数据表模型。下面,我们将这几个参数作为查询条件,在mysql数据库中进行数据查询:

project.findAll({where: {}})来进行多条件查询:

// 数据表模型.findAll()

var schoolInfo = await SchoolArea.findAll({

where:{

//字段名:查询条件参数

area: schoolArea,

grade: grade,

school: keyword

}

});

通过findAll()查询数据返回的结果是一个数组,其中的每个元素是查询到的每条数据的对象,格式如下:

[ Instance { // 第一条数据对象

0|app | dataValues:

0|app | { id: '0',

0|app | area: 'xx',

0|app | grade: 'xx',

0|app | school: 'xx',

0|app | residence: 'xx' },

0|app | _previousDataValues:

0|app | { ... },

0|app | _changed: {},

0|app | '$modelOptions':

0|app | { timestamps: true,

0|app | instanceMethods: {},

0|app | ...

0|app | hasPrimaryKeys: true },

0|app | '$options':

0|app | {... },

0|app | hasPrimaryKeys: true,

0|app | __eagerlyLoadedAssociations: [],

0|app | isNewRecord: false },

// 第二条数据对象

Instance{

...

}

]

通过Koa Context对象返回查询结果:

ctx.body(等同于ctx.response.body)作为http请求的响应主体,可以携带响应数据,返回给小程序客户端,在小程序客户端通过wx.request{ ..., success: function(res){ res.data }}获取ctx.body的响应数据(参见本文第一段代码)。

// 服务器nodejs

ctx.body={

data: {downloads: schoolInfo},

其他你需要的数据...

}

转载地址:https://blog.csdn.net/weixin_33800724/article/details/111962202 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:wget 连接超时_wget 这玩意要超时重试多少次啊?
下一篇:python动态类型的坑_Python学习笔记6——动态类型

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月24日 12时56分10秒